@atproto/lex-schema 0.0.9 → 0.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +41 -0
- package/LICENSE.txt +1 -1
- package/dist/core/$type.d.ts +11 -0
- package/dist/core/$type.d.ts.map +1 -1
- package/dist/core/$type.js +4 -0
- package/dist/core/$type.js.map +1 -1
- package/dist/core/schema.d.ts +31 -24
- package/dist/core/schema.d.ts.map +1 -1
- package/dist/core/schema.js +38 -8
- package/dist/core/schema.js.map +1 -1
- package/dist/core/string-format.d.ts +35 -35
- package/dist/core/string-format.d.ts.map +1 -1
- package/dist/core/string-format.js +49 -91
- package/dist/core/string-format.js.map +1 -1
- package/dist/core/validation-error.d.ts +1 -1
- package/dist/core/validation-issue.js +1 -1
- package/dist/core/validation-issue.js.map +1 -1
- package/dist/core/validator.d.ts +53 -32
- package/dist/core/validator.d.ts.map +1 -1
- package/dist/core/validator.js +18 -22
- package/dist/core/validator.js.map +1 -1
- package/dist/external.d.ts +0 -85
- package/dist/external.d.ts.map +1 -1
- package/dist/external.js +0 -164
- package/dist/external.js.map +1 -1
- package/dist/helpers.d.ts +13 -5
- package/dist/helpers.d.ts.map +1 -1
- package/dist/helpers.js +4 -4
- package/dist/helpers.js.map +1 -1
- package/dist/schema/array.d.ts +9 -5
- package/dist/schema/array.d.ts.map +1 -1
- package/dist/schema/array.js +14 -5
- package/dist/schema/array.js.map +1 -1
- package/dist/schema/blob.d.ts +9 -7
- package/dist/schema/blob.d.ts.map +1 -1
- package/dist/schema/blob.js +9 -5
- package/dist/schema/blob.js.map +1 -1
- package/dist/schema/boolean.d.ts +3 -7
- package/dist/schema/boolean.d.ts.map +1 -1
- package/dist/schema/boolean.js +6 -7
- package/dist/schema/boolean.js.map +1 -1
- package/dist/schema/bytes.d.ts +3 -2
- package/dist/schema/bytes.d.ts.map +1 -1
- package/dist/schema/bytes.js +7 -3
- package/dist/schema/bytes.js.map +1 -1
- package/dist/schema/cid.d.ts +7 -7
- package/dist/schema/cid.d.ts.map +1 -1
- package/dist/schema/cid.js +5 -1
- package/dist/schema/cid.js.map +1 -1
- package/dist/schema/custom.d.ts +6 -5
- package/dist/schema/custom.d.ts.map +1 -1
- package/dist/schema/custom.js +10 -4
- package/dist/schema/custom.js.map +1 -1
- package/dist/schema/dict.d.ts +8 -8
- package/dist/schema/dict.d.ts.map +1 -1
- package/dist/schema/dict.js +11 -2
- package/dist/schema/dict.js.map +1 -1
- package/dist/schema/discriminated-union.d.ts +21 -14
- package/dist/schema/discriminated-union.d.ts.map +1 -1
- package/dist/schema/discriminated-union.js +7 -0
- package/dist/schema/discriminated-union.js.map +1 -1
- package/dist/schema/enum.d.ts +7 -9
- package/dist/schema/enum.d.ts.map +1 -1
- package/dist/schema/enum.js +8 -4
- package/dist/schema/enum.js.map +1 -1
- package/dist/schema/integer.d.ts +5 -5
- package/dist/schema/integer.d.ts.map +1 -1
- package/dist/schema/integer.js +9 -5
- package/dist/schema/integer.js.map +1 -1
- package/dist/schema/intersection.d.ts +4 -4
- package/dist/schema/intersection.d.ts.map +1 -1
- package/dist/schema/intersection.js +5 -0
- package/dist/schema/intersection.js.map +1 -1
- package/dist/schema/literal.d.ts +6 -9
- package/dist/schema/literal.d.ts.map +1 -1
- package/dist/schema/literal.js +7 -4
- package/dist/schema/literal.js.map +1 -1
- package/dist/schema/never.d.ts +3 -2
- package/dist/schema/never.d.ts.map +1 -1
- package/dist/schema/never.js +5 -1
- package/dist/schema/never.js.map +1 -1
- package/dist/schema/null.d.ts +4 -3
- package/dist/schema/null.d.ts.map +1 -1
- package/dist/schema/null.js +6 -4
- package/dist/schema/null.js.map +1 -1
- package/dist/schema/nullable.d.ts +6 -5
- package/dist/schema/nullable.d.ts.map +1 -1
- package/dist/schema/nullable.js +9 -5
- package/dist/schema/nullable.js.map +1 -1
- package/dist/schema/object.d.ts +10 -8
- package/dist/schema/object.d.ts.map +1 -1
- package/dist/schema/object.js +11 -3
- package/dist/schema/object.js.map +1 -1
- package/dist/schema/optional.d.ts +7 -5
- package/dist/schema/optional.d.ts.map +1 -1
- package/dist/schema/optional.js +14 -6
- package/dist/schema/optional.js.map +1 -1
- package/dist/schema/params.d.ts +26 -13
- package/dist/schema/params.d.ts.map +1 -1
- package/dist/schema/params.js +47 -25
- package/dist/schema/params.js.map +1 -1
- package/dist/schema/payload.d.ts +12 -9
- package/dist/schema/payload.d.ts.map +1 -1
- package/dist/schema/payload.js +11 -0
- package/dist/schema/payload.js.map +1 -1
- package/dist/schema/permission-set.d.ts +1 -0
- package/dist/schema/permission-set.d.ts.map +1 -1
- package/dist/schema/permission-set.js +5 -0
- package/dist/schema/permission-set.js.map +1 -1
- package/dist/schema/permission.d.ts +6 -5
- package/dist/schema/permission.d.ts.map +1 -1
- package/dist/schema/permission.js +5 -0
- package/dist/schema/permission.js.map +1 -1
- package/dist/schema/procedure.d.ts +2 -1
- package/dist/schema/procedure.d.ts.map +1 -1
- package/dist/schema/procedure.js +5 -0
- package/dist/schema/procedure.js.map +1 -1
- package/dist/schema/query.d.ts +2 -1
- package/dist/schema/query.d.ts.map +1 -1
- package/dist/schema/query.js +5 -0
- package/dist/schema/query.js.map +1 -1
- package/dist/schema/record.d.ts +48 -30
- package/dist/schema/record.d.ts.map +1 -1
- package/dist/schema/record.js +12 -9
- package/dist/schema/record.js.map +1 -1
- package/dist/schema/ref.d.ts +9 -6
- package/dist/schema/ref.d.ts.map +1 -1
- package/dist/schema/ref.js +9 -16
- package/dist/schema/ref.js.map +1 -1
- package/dist/schema/refine.d.ts +4 -4
- package/dist/schema/refine.d.ts.map +1 -1
- package/dist/schema/refine.js.map +1 -1
- package/dist/schema/regexp.d.ts +4 -3
- package/dist/schema/regexp.d.ts.map +1 -1
- package/dist/schema/regexp.js +5 -0
- package/dist/schema/regexp.js.map +1 -1
- package/dist/schema/string.d.ts +7 -8
- package/dist/schema/string.d.ts.map +1 -1
- package/dist/schema/string.js +13 -19
- package/dist/schema/string.js.map +1 -1
- package/dist/schema/subscription.d.ts +2 -1
- package/dist/schema/subscription.d.ts.map +1 -1
- package/dist/schema/subscription.js +5 -0
- package/dist/schema/subscription.js.map +1 -1
- package/dist/schema/token.d.ts +6 -5
- package/dist/schema/token.d.ts.map +1 -1
- package/dist/schema/token.js +5 -0
- package/dist/schema/token.js.map +1 -1
- package/dist/schema/typed-object.d.ts +43 -26
- package/dist/schema/typed-object.d.ts.map +1 -1
- package/dist/schema/typed-object.js +6 -3
- package/dist/schema/typed-object.js.map +1 -1
- package/dist/schema/typed-ref.d.ts +16 -25
- package/dist/schema/typed-ref.d.ts.map +1 -1
- package/dist/schema/typed-ref.js +7 -17
- package/dist/schema/typed-ref.js.map +1 -1
- package/dist/schema/typed-union.d.ts +9 -21
- package/dist/schema/typed-union.d.ts.map +1 -1
- package/dist/schema/typed-union.js +15 -11
- package/dist/schema/typed-union.js.map +1 -1
- package/dist/schema/union.d.ts +6 -6
- package/dist/schema/union.d.ts.map +1 -1
- package/dist/schema/union.js +7 -5
- package/dist/schema/union.js.map +1 -1
- package/dist/schema/unknown-object.d.ts +5 -4
- package/dist/schema/unknown-object.d.ts.map +1 -1
- package/dist/schema/unknown-object.js +5 -1
- package/dist/schema/unknown-object.js.map +1 -1
- package/dist/schema/unknown.d.ts +3 -2
- package/dist/schema/unknown.d.ts.map +1 -1
- package/dist/schema/unknown.js +5 -1
- package/dist/schema/unknown.js.map +1 -1
- package/dist/schema/with-default.d.ts +9 -0
- package/dist/schema/with-default.d.ts.map +1 -0
- package/dist/schema/with-default.js +27 -0
- package/dist/schema/with-default.js.map +1 -0
- package/dist/schema.d.ts +2 -2
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +2 -4
- package/dist/schema.js.map +1 -1
- package/dist/util/assertion-util.d.ts +0 -6
- package/dist/util/assertion-util.d.ts.map +1 -1
- package/dist/util/assertion-util.js +0 -28
- package/dist/util/assertion-util.js.map +1 -1
- package/dist/util/memoize.d.ts +2 -2
- package/dist/util/memoize.d.ts.map +1 -1
- package/dist/util/memoize.js +23 -39
- package/dist/util/memoize.js.map +1 -1
- package/package.json +3 -3
- package/src/core/$type.test.ts +20 -0
- package/src/core/$type.ts +30 -0
- package/src/core/schema.ts +86 -38
- package/src/core/string-format.ts +119 -158
- package/src/core/validation-issue.ts +1 -1
- package/src/core/validator.ts +93 -53
- package/src/external.ts +0 -404
- package/src/helpers.test.ts +22 -21
- package/src/helpers.ts +19 -14
- package/src/schema/array.test.ts +38 -40
- package/src/schema/array.ts +35 -13
- package/src/schema/blob.test.ts +21 -21
- package/src/schema/blob.ts +19 -17
- package/src/schema/boolean.test.ts +9 -8
- package/src/schema/boolean.ts +7 -13
- package/src/schema/bytes.test.ts +13 -13
- package/src/schema/bytes.ts +13 -8
- package/src/schema/cid.test.ts +3 -3
- package/src/schema/cid.ts +13 -12
- package/src/schema/custom.test.ts +26 -26
- package/src/schema/custom.ts +20 -13
- package/src/schema/dict.test.ts +21 -39
- package/src/schema/dict.ts +28 -19
- package/src/schema/discriminated-union.test.ts +128 -128
- package/src/schema/discriminated-union.ts +45 -26
- package/src/schema/enum.test.ts +17 -16
- package/src/schema/enum.ts +16 -16
- package/src/schema/integer.test.ts +22 -21
- package/src/schema/integer.ts +12 -9
- package/src/schema/intersection.test.ts +10 -10
- package/src/schema/intersection.ts +17 -14
- package/src/schema/literal.test.ts +35 -34
- package/src/schema/literal.ts +12 -15
- package/src/schema/never.test.ts +5 -5
- package/src/schema/never.ts +7 -2
- package/src/schema/null.test.ts +3 -3
- package/src/schema/null.ts +9 -9
- package/src/schema/nullable.test.ts +31 -42
- package/src/schema/nullable.ts +17 -9
- package/src/schema/object.test.ts +10 -12
- package/src/schema/object.ts +27 -18
- package/src/schema/optional.test.ts +21 -28
- package/src/schema/optional.ts +27 -10
- package/src/schema/params.test.ts +471 -47
- package/src/schema/params.ts +74 -38
- package/src/schema/payload.test.ts +150 -156
- package/src/schema/payload.ts +35 -19
- package/src/schema/permission-set.test.ts +206 -273
- package/src/schema/permission-set.ts +8 -0
- package/src/schema/permission.test.ts +177 -177
- package/src/schema/permission.ts +13 -5
- package/src/schema/procedure.test.ts +183 -242
- package/src/schema/procedure.ts +18 -5
- package/src/schema/query.test.ts +186 -200
- package/src/schema/query.ts +16 -4
- package/src/schema/record.test.ts +121 -101
- package/src/schema/record.ts +74 -40
- package/src/schema/ref.test.ts +101 -118
- package/src/schema/ref.ts +33 -28
- package/src/schema/refine.test.ts +28 -28
- package/src/schema/refine.ts +23 -20
- package/src/schema/regexp.test.ts +29 -33
- package/src/schema/regexp.ts +11 -7
- package/src/schema/string.test.ts +35 -35
- package/src/schema/string.ts +24 -33
- package/src/schema/subscription.test.ts +259 -387
- package/src/schema/subscription.ts +16 -4
- package/src/schema/token.test.ts +47 -324
- package/src/schema/token.ts +14 -7
- package/src/schema/typed-object.test.ts +98 -81
- package/src/schema/typed-object.ts +68 -33
- package/src/schema/typed-ref.test.ts +206 -234
- package/src/schema/typed-ref.ts +40 -42
- package/src/schema/typed-union.test.ts +40 -64
- package/src/schema/typed-union.ts +36 -58
- package/src/schema/union.test.ts +17 -27
- package/src/schema/union.ts +20 -16
- package/src/schema/unknown-object.test.ts +8 -8
- package/src/schema/unknown-object.ts +9 -7
- package/src/schema/unknown.test.ts +4 -4
- package/src/schema/unknown.ts +7 -5
- package/src/schema/with-default.ts +35 -0
- package/src/schema.ts +2 -6
- package/src/util/assertion-util.ts +0 -39
- package/src/util/memoize.ts +26 -46
- package/dist/schema/_parameters.d.ts +0 -17
- package/dist/schema/_parameters.d.ts.map +0 -1
- package/dist/schema/_parameters.js +0 -20
- package/dist/schema/_parameters.js.map +0 -1
- package/src/schema/_parameters.test.ts +0 -417
- package/src/schema/_parameters.ts +0 -26
package/src/schema/permission.ts
CHANGED
|
@@ -1,13 +1,21 @@
|
|
|
1
|
-
import { Params } from './
|
|
1
|
+
import { Params } from './params.js'
|
|
2
2
|
|
|
3
3
|
export type PermissionOptions = Params
|
|
4
4
|
|
|
5
5
|
export class Permission<
|
|
6
|
-
const
|
|
7
|
-
const
|
|
6
|
+
const TResource extends string = any,
|
|
7
|
+
const TOptions extends PermissionOptions = any,
|
|
8
8
|
> {
|
|
9
9
|
constructor(
|
|
10
|
-
readonly resource:
|
|
11
|
-
readonly options:
|
|
10
|
+
readonly resource: TResource,
|
|
11
|
+
readonly options: TOptions,
|
|
12
12
|
) {}
|
|
13
13
|
}
|
|
14
|
+
|
|
15
|
+
/*@__NO_SIDE_EFFECTS__*/
|
|
16
|
+
export function permission<
|
|
17
|
+
const R extends string,
|
|
18
|
+
const O extends PermissionOptions,
|
|
19
|
+
>(resource: R, options: PermissionOptions & O = {} as O) {
|
|
20
|
+
return new Permission<R, O>(resource, options)
|
|
21
|
+
}
|
|
@@ -1,315 +1,259 @@
|
|
|
1
1
|
import { describe, expect, it } from 'vitest'
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
2
|
+
import { object } from './object.js'
|
|
3
|
+
import { params } from './params.js'
|
|
4
|
+
import { payload } from './payload.js'
|
|
5
|
+
import { procedure } from './procedure.js'
|
|
6
|
+
import { string } from './string.js'
|
|
7
7
|
|
|
8
8
|
describe('Procedure', () => {
|
|
9
9
|
describe('basic construction', () => {
|
|
10
10
|
it('creates a procedure with all parameters', () => {
|
|
11
|
-
const nsid = 'com.example.createPost'
|
|
12
|
-
const parameters = new ParamsSchema({})
|
|
13
|
-
const input = new Payload('application/json', undefined)
|
|
14
|
-
const output = new Payload('application/json', undefined)
|
|
15
11
|
const errors = ['InvalidRequest', 'Unauthorized'] as const
|
|
16
12
|
|
|
17
|
-
const
|
|
13
|
+
const createPost = procedure(
|
|
14
|
+
'com.example.createPost',
|
|
15
|
+
params(),
|
|
16
|
+
payload('application/json', undefined),
|
|
17
|
+
payload('application/json', undefined),
|
|
18
|
+
errors,
|
|
19
|
+
)
|
|
18
20
|
|
|
19
|
-
expect(
|
|
20
|
-
expect(
|
|
21
|
-
expect(
|
|
22
|
-
expect(
|
|
23
|
-
expect(
|
|
21
|
+
expect(createPost.nsid).toBe('com.example.createPost')
|
|
22
|
+
expect(createPost.parameters).toEqual(params())
|
|
23
|
+
expect(createPost.input).toEqual(payload('application/json', undefined))
|
|
24
|
+
expect(createPost.output).toEqual(payload('application/json', undefined))
|
|
25
|
+
expect(createPost.errors).toBe(errors)
|
|
24
26
|
})
|
|
25
27
|
|
|
26
28
|
it('creates a procedure without errors', () => {
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const procedure = new Procedure(
|
|
33
|
-
nsid,
|
|
34
|
-
parameters,
|
|
35
|
-
input,
|
|
36
|
-
output,
|
|
29
|
+
const doSomething = procedure(
|
|
30
|
+
'com.example.doSomething',
|
|
31
|
+
params(),
|
|
32
|
+
payload('application/json', undefined),
|
|
33
|
+
payload('application/json', undefined),
|
|
37
34
|
undefined,
|
|
38
35
|
)
|
|
39
36
|
|
|
40
|
-
expect(
|
|
41
|
-
expect(
|
|
42
|
-
expect(
|
|
43
|
-
expect(
|
|
44
|
-
expect(
|
|
37
|
+
expect(doSomething.nsid).toBe('com.example.doSomething')
|
|
38
|
+
expect(doSomething.parameters).toEqual(params())
|
|
39
|
+
expect(doSomething.input).toEqual(payload('application/json', undefined))
|
|
40
|
+
expect(doSomething.output).toEqual(payload('application/json', undefined))
|
|
41
|
+
expect(doSomething.errors).toBeUndefined()
|
|
45
42
|
})
|
|
46
43
|
|
|
47
44
|
it('creates a procedure with empty errors array', () => {
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
45
|
+
const performAction = procedure(
|
|
46
|
+
'com.example.performAction',
|
|
47
|
+
params(),
|
|
48
|
+
payload('application/json', undefined),
|
|
49
|
+
payload('application/json', undefined),
|
|
50
|
+
[] as const,
|
|
51
|
+
)
|
|
55
52
|
|
|
56
|
-
expect(
|
|
53
|
+
expect(performAction.errors).toEqual([])
|
|
57
54
|
})
|
|
58
55
|
})
|
|
59
56
|
|
|
60
57
|
describe('with parameters schema', () => {
|
|
61
58
|
it('creates a procedure with query parameters', () => {
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
const procedure = new Procedure(
|
|
71
|
-
nsid,
|
|
72
|
-
parameters,
|
|
73
|
-
input,
|
|
74
|
-
output,
|
|
59
|
+
const listPosts = procedure(
|
|
60
|
+
'com.example.listPosts',
|
|
61
|
+
params({
|
|
62
|
+
limit: string(),
|
|
63
|
+
cursor: string(),
|
|
64
|
+
}),
|
|
65
|
+
payload(undefined, undefined),
|
|
66
|
+
payload('application/json', undefined),
|
|
75
67
|
undefined,
|
|
76
68
|
)
|
|
77
69
|
|
|
78
|
-
expect(
|
|
79
|
-
expect(
|
|
80
|
-
expect(procedure.parameters.validators).toHaveProperty('cursor')
|
|
70
|
+
expect(listPosts.parameters.shape).toHaveProperty('limit')
|
|
71
|
+
expect(listPosts.parameters.shape).toHaveProperty('cursor')
|
|
81
72
|
})
|
|
82
73
|
|
|
83
74
|
it('creates a procedure with empty parameters', () => {
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
const procedure = new Procedure(
|
|
90
|
-
nsid,
|
|
91
|
-
parameters,
|
|
92
|
-
input,
|
|
93
|
-
output,
|
|
75
|
+
const myProcedure = procedure(
|
|
76
|
+
'com.example.action',
|
|
77
|
+
params(),
|
|
78
|
+
payload('application/json', undefined),
|
|
79
|
+
payload('application/json', undefined),
|
|
94
80
|
undefined,
|
|
95
81
|
)
|
|
96
82
|
|
|
97
|
-
expect(
|
|
98
|
-
expect(Object.keys(procedure.parameters.validators)).toHaveLength(0)
|
|
83
|
+
expect(Object.keys(myProcedure.parameters.shape)).toHaveLength(0)
|
|
99
84
|
})
|
|
100
85
|
})
|
|
101
86
|
|
|
102
87
|
describe('with input payload', () => {
|
|
103
88
|
it('creates a procedure with JSON input', () => {
|
|
104
|
-
const
|
|
105
|
-
|
|
106
|
-
const inputSchema = new ObjectSchema({
|
|
107
|
-
text: new StringSchema({}),
|
|
89
|
+
const inputSchema = object({
|
|
90
|
+
text: string(),
|
|
108
91
|
})
|
|
109
|
-
const input = new Payload('application/json', inputSchema)
|
|
110
|
-
const output = new Payload('application/json', undefined)
|
|
111
92
|
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
93
|
+
const myProcedure = procedure(
|
|
94
|
+
'com.example.createPost',
|
|
95
|
+
params(),
|
|
96
|
+
payload('application/json', inputSchema),
|
|
97
|
+
payload('application/json', undefined),
|
|
117
98
|
undefined,
|
|
118
99
|
)
|
|
119
100
|
|
|
120
|
-
expect(
|
|
121
|
-
expect(
|
|
101
|
+
expect(myProcedure.input.encoding).toBe('application/json')
|
|
102
|
+
expect(myProcedure.input.schema).toBe(inputSchema)
|
|
122
103
|
})
|
|
123
104
|
|
|
124
105
|
it('creates a procedure with text input', () => {
|
|
125
|
-
const
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
const procedure = new Procedure(
|
|
131
|
-
nsid,
|
|
132
|
-
parameters,
|
|
133
|
-
input,
|
|
134
|
-
output,
|
|
106
|
+
const myProcedure = procedure(
|
|
107
|
+
'com.example.uploadText',
|
|
108
|
+
params(),
|
|
109
|
+
payload('text/plain', undefined),
|
|
110
|
+
payload('application/json', undefined),
|
|
135
111
|
undefined,
|
|
136
112
|
)
|
|
137
113
|
|
|
138
|
-
expect(
|
|
139
|
-
expect(
|
|
114
|
+
expect(myProcedure.input.encoding).toBe('text/plain')
|
|
115
|
+
expect(myProcedure.input.schema).toBeUndefined()
|
|
140
116
|
})
|
|
141
117
|
|
|
142
118
|
it('creates a procedure with binary input', () => {
|
|
143
|
-
const
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
const procedure = new Procedure(
|
|
149
|
-
nsid,
|
|
150
|
-
parameters,
|
|
151
|
-
input,
|
|
152
|
-
output,
|
|
119
|
+
const myProcedure = procedure(
|
|
120
|
+
'com.example.uploadBlob',
|
|
121
|
+
params(),
|
|
122
|
+
payload('application/octet-stream', undefined),
|
|
123
|
+
payload('application/json', undefined),
|
|
153
124
|
undefined,
|
|
154
125
|
)
|
|
155
126
|
|
|
156
|
-
expect(
|
|
127
|
+
expect(myProcedure.input.encoding).toBe('application/octet-stream')
|
|
157
128
|
})
|
|
158
129
|
|
|
159
130
|
it('creates a procedure with no input', () => {
|
|
160
|
-
const
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
const procedure = new Procedure(
|
|
166
|
-
nsid,
|
|
167
|
-
parameters,
|
|
168
|
-
input,
|
|
169
|
-
output,
|
|
131
|
+
const myProcedure = procedure(
|
|
132
|
+
'com.example.action',
|
|
133
|
+
params(),
|
|
134
|
+
payload(undefined, undefined),
|
|
135
|
+
payload('application/json', undefined),
|
|
170
136
|
undefined,
|
|
171
137
|
)
|
|
172
138
|
|
|
173
|
-
expect(
|
|
174
|
-
expect(
|
|
139
|
+
expect(myProcedure.input.encoding).toBeUndefined()
|
|
140
|
+
expect(myProcedure.input.schema).toBeUndefined()
|
|
175
141
|
})
|
|
176
142
|
})
|
|
177
143
|
|
|
178
144
|
describe('with output payload', () => {
|
|
179
145
|
it('creates a procedure with JSON output', () => {
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
const outputSchema = new ObjectSchema({
|
|
184
|
-
uri: new StringSchema({}),
|
|
185
|
-
cid: new StringSchema({}),
|
|
146
|
+
const outputSchema = object({
|
|
147
|
+
uri: string(),
|
|
148
|
+
cid: string(),
|
|
186
149
|
})
|
|
187
|
-
const output = new Payload('application/json', outputSchema)
|
|
188
150
|
|
|
189
|
-
const
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
151
|
+
const myProcedure = procedure(
|
|
152
|
+
'com.example.getPost',
|
|
153
|
+
params(),
|
|
154
|
+
payload(undefined, undefined),
|
|
155
|
+
payload('application/json', outputSchema),
|
|
194
156
|
undefined,
|
|
195
157
|
)
|
|
196
158
|
|
|
197
|
-
expect(
|
|
198
|
-
expect(
|
|
159
|
+
expect(myProcedure.output.encoding).toBe('application/json')
|
|
160
|
+
expect(myProcedure.output.schema).toBe(outputSchema)
|
|
199
161
|
})
|
|
200
162
|
|
|
201
163
|
it('creates a procedure with text output', () => {
|
|
202
|
-
const
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
const procedure = new Procedure(
|
|
208
|
-
nsid,
|
|
209
|
-
parameters,
|
|
210
|
-
input,
|
|
211
|
-
output,
|
|
164
|
+
const myProcedure = procedure(
|
|
165
|
+
'com.example.export',
|
|
166
|
+
params(),
|
|
167
|
+
payload(undefined, undefined),
|
|
168
|
+
payload('text/plain', undefined),
|
|
212
169
|
undefined,
|
|
213
170
|
)
|
|
214
171
|
|
|
215
|
-
expect(
|
|
172
|
+
expect(myProcedure.output.encoding).toBe('text/plain')
|
|
216
173
|
})
|
|
217
174
|
|
|
218
175
|
it('creates a procedure with binary output', () => {
|
|
219
|
-
const
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
const procedure = new Procedure(
|
|
225
|
-
nsid,
|
|
226
|
-
parameters,
|
|
227
|
-
input,
|
|
228
|
-
output,
|
|
176
|
+
const myProcedure = procedure(
|
|
177
|
+
'com.example.download',
|
|
178
|
+
params(),
|
|
179
|
+
payload(undefined, undefined),
|
|
180
|
+
payload('application/octet-stream', undefined),
|
|
229
181
|
undefined,
|
|
230
182
|
)
|
|
231
183
|
|
|
232
|
-
expect(
|
|
184
|
+
expect(myProcedure.output.encoding).toBe('application/octet-stream')
|
|
233
185
|
})
|
|
234
186
|
|
|
235
187
|
it('creates a procedure with no output', () => {
|
|
236
|
-
const
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
const procedure = new Procedure(
|
|
242
|
-
nsid,
|
|
243
|
-
parameters,
|
|
244
|
-
input,
|
|
245
|
-
output,
|
|
188
|
+
const myProcedure = procedure(
|
|
189
|
+
'com.example.deletePost',
|
|
190
|
+
params(),
|
|
191
|
+
payload('application/json', undefined),
|
|
192
|
+
payload(undefined, undefined),
|
|
246
193
|
undefined,
|
|
247
194
|
)
|
|
248
195
|
|
|
249
|
-
expect(
|
|
250
|
-
expect(
|
|
196
|
+
expect(myProcedure.output.encoding).toBeUndefined()
|
|
197
|
+
expect(myProcedure.output.schema).toBeUndefined()
|
|
251
198
|
})
|
|
252
199
|
})
|
|
253
200
|
|
|
254
201
|
describe('with error definitions', () => {
|
|
255
202
|
it('creates a procedure with single error', () => {
|
|
256
|
-
const
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
203
|
+
const myProcedure = procedure(
|
|
204
|
+
'com.example.action',
|
|
205
|
+
params(),
|
|
206
|
+
payload('application/json', undefined),
|
|
207
|
+
payload('application/json', undefined),
|
|
208
|
+
['InvalidRequest'] as const,
|
|
209
|
+
)
|
|
263
210
|
|
|
264
|
-
expect(
|
|
211
|
+
expect(myProcedure.errors).toEqual(['InvalidRequest'])
|
|
265
212
|
})
|
|
266
213
|
|
|
267
214
|
it('creates a procedure with multiple errors', () => {
|
|
268
|
-
const
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
'InvalidRequest',
|
|
274
|
-
|
|
275
|
-
'RateLimitExceeded',
|
|
276
|
-
] as const
|
|
277
|
-
|
|
278
|
-
const procedure = new Procedure(nsid, parameters, input, output, errors)
|
|
215
|
+
const myProcedure = procedure(
|
|
216
|
+
'com.example.createPost',
|
|
217
|
+
params(),
|
|
218
|
+
payload('application/json', undefined),
|
|
219
|
+
payload('application/json', undefined),
|
|
220
|
+
['InvalidRequest', 'Unauthorized', 'RateLimitExceeded'] as const,
|
|
221
|
+
)
|
|
279
222
|
|
|
280
|
-
expect(
|
|
281
|
-
expect(
|
|
282
|
-
expect(
|
|
283
|
-
expect(
|
|
223
|
+
expect(myProcedure.errors).toHaveLength(3)
|
|
224
|
+
expect(myProcedure.errors).toContain('InvalidRequest')
|
|
225
|
+
expect(myProcedure.errors).toContain('Unauthorized')
|
|
226
|
+
expect(myProcedure.errors).toContain('RateLimitExceeded')
|
|
284
227
|
})
|
|
285
228
|
})
|
|
286
229
|
|
|
287
230
|
describe('property access', () => {
|
|
288
231
|
it('provides access to all properties', () => {
|
|
289
|
-
const nsid = 'com.example.test'
|
|
290
|
-
const parameters = new ParamsSchema({})
|
|
291
|
-
const input = new Payload('application/json', undefined)
|
|
292
|
-
const output = new Payload('application/json', undefined)
|
|
293
232
|
const errors = ['Error1', 'Error2'] as const
|
|
294
233
|
|
|
295
|
-
const
|
|
234
|
+
const myProcedure = procedure(
|
|
235
|
+
'com.example.test',
|
|
236
|
+
params(),
|
|
237
|
+
payload('application/json', undefined),
|
|
238
|
+
payload('application/json', undefined),
|
|
239
|
+
errors,
|
|
240
|
+
)
|
|
296
241
|
|
|
297
|
-
|
|
298
|
-
expect(
|
|
299
|
-
expect(
|
|
300
|
-
expect(
|
|
301
|
-
expect(
|
|
302
|
-
expect(procedure.errors).toBe(errors)
|
|
242
|
+
expect(myProcedure.nsid).toBe('com.example.test')
|
|
243
|
+
expect(myProcedure.parameters).toEqual(params())
|
|
244
|
+
expect(myProcedure.input).toEqual(payload('application/json', undefined))
|
|
245
|
+
expect(myProcedure.output).toEqual(payload('application/json', undefined))
|
|
246
|
+
expect(myProcedure.errors).toBe(errors)
|
|
303
247
|
})
|
|
304
248
|
|
|
305
249
|
it('maintains reference equality for complex properties', () => {
|
|
306
|
-
const parameters =
|
|
307
|
-
const inputSchema =
|
|
308
|
-
const outputSchema =
|
|
309
|
-
const input =
|
|
310
|
-
const output =
|
|
250
|
+
const parameters = params({ test: string() })
|
|
251
|
+
const inputSchema = object({ field: string() })
|
|
252
|
+
const outputSchema = object({ result: string() })
|
|
253
|
+
const input = payload('application/json', inputSchema)
|
|
254
|
+
const output = payload('application/json', outputSchema)
|
|
311
255
|
|
|
312
|
-
const
|
|
256
|
+
const myProcedure = procedure(
|
|
313
257
|
'com.example.test',
|
|
314
258
|
parameters,
|
|
315
259
|
input,
|
|
@@ -318,66 +262,63 @@ describe('Procedure', () => {
|
|
|
318
262
|
)
|
|
319
263
|
|
|
320
264
|
// Verify references are maintained
|
|
321
|
-
expect(
|
|
322
|
-
expect(
|
|
323
|
-
expect(
|
|
324
|
-
expect(
|
|
325
|
-
expect(
|
|
265
|
+
expect(myProcedure.parameters).toBe(parameters)
|
|
266
|
+
expect(myProcedure.input).toBe(input)
|
|
267
|
+
expect(myProcedure.output).toBe(output)
|
|
268
|
+
expect(myProcedure.input.schema).toBe(inputSchema)
|
|
269
|
+
expect(myProcedure.output.schema).toBe(outputSchema)
|
|
326
270
|
})
|
|
327
271
|
})
|
|
328
272
|
|
|
329
273
|
describe('complex scenarios', () => {
|
|
330
274
|
it('creates a fully-featured procedure', () => {
|
|
331
|
-
const
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
})
|
|
335
|
-
const inputSchema = new ObjectSchema({
|
|
336
|
-
text: new StringSchema({}),
|
|
337
|
-
mentions: new StringSchema({}),
|
|
275
|
+
const inputSchema = object({
|
|
276
|
+
text: string(),
|
|
277
|
+
mentions: string(),
|
|
338
278
|
})
|
|
339
|
-
const outputSchema =
|
|
340
|
-
messageId:
|
|
341
|
-
timestamp:
|
|
279
|
+
const outputSchema = object({
|
|
280
|
+
messageId: string(),
|
|
281
|
+
timestamp: string(),
|
|
342
282
|
})
|
|
343
|
-
const input = new Payload('application/json', inputSchema)
|
|
344
|
-
const output = new Payload('application/json', outputSchema)
|
|
345
283
|
const errors = [
|
|
346
284
|
'ConversationNotFound',
|
|
347
285
|
'MessageTooLong',
|
|
348
286
|
'RateLimitExceeded',
|
|
349
287
|
] as const
|
|
350
288
|
|
|
351
|
-
const
|
|
289
|
+
const myProcedure = procedure(
|
|
290
|
+
'com.example.chat.sendMessage',
|
|
291
|
+
params({
|
|
292
|
+
conversationId: string(),
|
|
293
|
+
}),
|
|
294
|
+
payload('application/json', inputSchema),
|
|
295
|
+
payload('application/json', outputSchema),
|
|
296
|
+
errors,
|
|
297
|
+
)
|
|
352
298
|
|
|
353
|
-
expect(
|
|
354
|
-
expect(
|
|
355
|
-
expect(
|
|
356
|
-
expect(
|
|
357
|
-
expect(
|
|
358
|
-
expect(
|
|
359
|
-
expect(
|
|
299
|
+
expect(myProcedure.nsid).toBe('com.example.chat.sendMessage')
|
|
300
|
+
expect(myProcedure.parameters.shape).toHaveProperty('conversationId')
|
|
301
|
+
expect(myProcedure.input.encoding).toBe('application/json')
|
|
302
|
+
expect(myProcedure.input.schema).toBe(inputSchema)
|
|
303
|
+
expect(myProcedure.output.encoding).toBe('application/json')
|
|
304
|
+
expect(myProcedure.output.schema).toBe(outputSchema)
|
|
305
|
+
expect(myProcedure.errors).toEqual(errors)
|
|
360
306
|
})
|
|
361
307
|
|
|
362
308
|
it('creates a minimal procedure', () => {
|
|
363
|
-
const
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
const procedure = new Procedure(
|
|
369
|
-
nsid,
|
|
370
|
-
parameters,
|
|
371
|
-
input,
|
|
372
|
-
output,
|
|
309
|
+
const myProcedure = procedure(
|
|
310
|
+
'com.example.ping',
|
|
311
|
+
params(),
|
|
312
|
+
payload(undefined, undefined),
|
|
313
|
+
payload(undefined, undefined),
|
|
373
314
|
undefined,
|
|
374
315
|
)
|
|
375
316
|
|
|
376
|
-
expect(
|
|
377
|
-
expect(Object.keys(
|
|
378
|
-
expect(
|
|
379
|
-
expect(
|
|
380
|
-
expect(
|
|
317
|
+
expect(myProcedure.nsid).toBe('com.example.ping')
|
|
318
|
+
expect(Object.keys(myProcedure.parameters.shape)).toHaveLength(0)
|
|
319
|
+
expect(myProcedure.input.encoding).toBeUndefined()
|
|
320
|
+
expect(myProcedure.output.encoding).toBeUndefined()
|
|
321
|
+
expect(myProcedure.errors).toBeUndefined()
|
|
381
322
|
})
|
|
382
323
|
})
|
|
383
324
|
})
|
package/src/schema/procedure.ts
CHANGED
|
@@ -3,11 +3,13 @@ import { ParamsSchema } from './params.js'
|
|
|
3
3
|
import { Payload } from './payload.js'
|
|
4
4
|
|
|
5
5
|
export class Procedure<
|
|
6
|
-
TNsid extends NsidString = NsidString,
|
|
7
|
-
TParameters extends ParamsSchema = ParamsSchema,
|
|
8
|
-
TInputPayload extends Payload = Payload,
|
|
9
|
-
TOutputPayload extends Payload = Payload,
|
|
10
|
-
TErrors extends undefined | readonly string[] =
|
|
6
|
+
const TNsid extends NsidString = NsidString,
|
|
7
|
+
const TParameters extends ParamsSchema = ParamsSchema,
|
|
8
|
+
const TInputPayload extends Payload = Payload,
|
|
9
|
+
const TOutputPayload extends Payload = Payload,
|
|
10
|
+
const TErrors extends undefined | readonly string[] =
|
|
11
|
+
| undefined
|
|
12
|
+
| readonly string[],
|
|
11
13
|
> {
|
|
12
14
|
readonly type = 'procedure' as const
|
|
13
15
|
|
|
@@ -19,3 +21,14 @@ export class Procedure<
|
|
|
19
21
|
readonly errors: TErrors,
|
|
20
22
|
) {}
|
|
21
23
|
}
|
|
24
|
+
|
|
25
|
+
/*@__NO_SIDE_EFFECTS__*/
|
|
26
|
+
export function procedure<
|
|
27
|
+
const N extends NsidString,
|
|
28
|
+
const P extends ParamsSchema,
|
|
29
|
+
const I extends Payload,
|
|
30
|
+
const O extends Payload,
|
|
31
|
+
const E extends undefined | readonly string[] = undefined,
|
|
32
|
+
>(nsid: N, parameters: P, input: I, output: O, errors: E = undefined as E) {
|
|
33
|
+
return new Procedure<N, P, I, O, E>(nsid, parameters, input, output, errors)
|
|
34
|
+
}
|