-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: update ucanto to invocation spec compatible result type (#272)
- Updates result type to use ucan invocation spec compatible representation - At the library layer (except of the schemas which needs to deal with optional fields) we now enforce no `null|undefined` values in Results and push towards use of unit value `{}` instead. - Mostly this guides users into a better and more extensible API design. - Please note that `false` and `""` are still allowed. -⚠️ above changes caused breaking API change on `.derives` methods than no longer return `true` or `Failure` but instead return `Result<{}, Failure>`. -⚠️ Error types no longer have `error: true` field which were in place to pattern match on ok / error.
- Loading branch information
Showing
44 changed files
with
1,275 additions
and
942 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import * as API from '@ucanto/interface' | ||
|
||
/** | ||
* Creates the success result containing given `value`. Throws if | ||
* `null` or `undefined` passed to encourage use of units instead. | ||
* | ||
* @template {{}|string|boolean|number} T | ||
* @param {T} value | ||
* @returns {{ok: T, value?:undefined}} | ||
*/ | ||
export const ok = value => { | ||
if (value == null) { | ||
throw new TypeError(`ok(${value}) is not allowed, consider ok({}) instead`) | ||
} else { | ||
return { ok: value } | ||
} | ||
} | ||
|
||
/** | ||
* Creates the failing result containing given `cause` of error. | ||
* Throws if `cause` is `null` or `undefined` to encourage | ||
* passing descriptive errors instead. | ||
* | ||
* @template {{}|string|boolean|number} X | ||
* @param {X} cause | ||
* @returns {{ok?:undefined, error:X}} | ||
*/ | ||
export const error = cause => { | ||
if (cause == null) { | ||
throw new TypeError( | ||
`error(${cause}) is not allowed, consider passing an error instead` | ||
) | ||
} else { | ||
return { error: cause } | ||
} | ||
} | ||
|
||
/** | ||
* Creates the failing result containing an error with a given | ||
* `message`. Unlike `error` function it creates a very generic | ||
* error with `message` & `stack` fields. The `error` function | ||
* is recommended over `fail` for all but the most basic use cases. | ||
* | ||
* @param {string} message | ||
* @returns {{error:API.Failure, ok?:undefined}} | ||
*/ | ||
export const fail = message => ({ error: new Failure(message) }) | ||
|
||
/** | ||
* @implements {API.Failure} | ||
*/ | ||
export class Failure extends Error { | ||
describe() { | ||
return this.toString() | ||
} | ||
get message() { | ||
return this.describe() | ||
} | ||
toJSON() { | ||
const { name, message, stack } = this | ||
return { name, message, stack } | ||
} | ||
} |
Oops, something went wrong.