@atproto/lex-schema 0.0.11 → 0.0.12
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 +19 -0
- package/dist/core/$type.d.ts +149 -0
- package/dist/core/$type.d.ts.map +1 -1
- package/dist/core/$type.js +44 -0
- package/dist/core/$type.js.map +1 -1
- package/dist/core/record-key.d.ts +44 -0
- package/dist/core/record-key.d.ts.map +1 -1
- package/dist/core/record-key.js +30 -0
- package/dist/core/record-key.js.map +1 -1
- package/dist/core/result.d.ts +85 -4
- package/dist/core/result.d.ts.map +1 -1
- package/dist/core/result.js +60 -4
- package/dist/core/result.js.map +1 -1
- package/dist/core/schema.d.ts +229 -2
- package/dist/core/schema.d.ts.map +1 -1
- package/dist/core/schema.js +197 -4
- package/dist/core/schema.js.map +1 -1
- package/dist/core/string-format.d.ts +244 -11
- package/dist/core/string-format.d.ts.map +1 -1
- package/dist/core/string-format.js +150 -0
- package/dist/core/string-format.js.map +1 -1
- package/dist/core/types.d.ts +90 -3
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/types.js.map +1 -1
- package/dist/core/validation-error.d.ts +60 -0
- package/dist/core/validation-error.d.ts.map +1 -1
- package/dist/core/validation-error.js +60 -0
- package/dist/core/validation-error.js.map +1 -1
- package/dist/core/validation-issue.d.ts +61 -0
- package/dist/core/validation-issue.d.ts.map +1 -1
- package/dist/core/validation-issue.js +51 -0
- package/dist/core/validation-issue.js.map +1 -1
- package/dist/core/validator.d.ts +347 -10
- package/dist/core/validator.d.ts.map +1 -1
- package/dist/core/validator.js +184 -3
- package/dist/core/validator.js.map +1 -1
- package/dist/helpers.d.ts +9 -24
- package/dist/helpers.d.ts.map +1 -1
- package/dist/helpers.js.map +1 -1
- package/dist/schema/array.d.ts +45 -0
- package/dist/schema/array.d.ts.map +1 -1
- package/dist/schema/array.js +14 -0
- package/dist/schema/array.js.map +1 -1
- package/dist/schema/blob.d.ts +46 -0
- package/dist/schema/blob.d.ts.map +1 -1
- package/dist/schema/blob.js +39 -0
- package/dist/schema/blob.js.map +1 -1
- package/dist/schema/boolean.d.ts +28 -0
- package/dist/schema/boolean.d.ts.map +1 -1
- package/dist/schema/boolean.js +28 -0
- package/dist/schema/boolean.js.map +1 -1
- package/dist/schema/bytes.d.ts +38 -0
- package/dist/schema/bytes.d.ts.map +1 -1
- package/dist/schema/bytes.js +32 -0
- package/dist/schema/bytes.js.map +1 -1
- package/dist/schema/cid.d.ts +38 -0
- package/dist/schema/cid.d.ts.map +1 -1
- package/dist/schema/cid.js +33 -0
- package/dist/schema/cid.js.map +1 -1
- package/dist/schema/custom.d.ts +66 -1
- package/dist/schema/custom.d.ts.map +1 -1
- package/dist/schema/custom.js +54 -0
- package/dist/schema/custom.js.map +1 -1
- package/dist/schema/dict.d.ts +44 -0
- package/dist/schema/dict.d.ts.map +1 -1
- package/dist/schema/dict.js +44 -0
- package/dist/schema/dict.js.map +1 -1
- package/dist/schema/discriminated-union.d.ts +58 -0
- package/dist/schema/discriminated-union.d.ts.map +1 -1
- package/dist/schema/discriminated-union.js +45 -0
- package/dist/schema/discriminated-union.js.map +1 -1
- package/dist/schema/enum.d.ts +48 -0
- package/dist/schema/enum.d.ts.map +1 -1
- package/dist/schema/enum.js +48 -0
- package/dist/schema/enum.js.map +1 -1
- package/dist/schema/integer.d.ts +42 -0
- package/dist/schema/integer.d.ts.map +1 -1
- package/dist/schema/integer.js +36 -0
- package/dist/schema/integer.js.map +1 -1
- package/dist/schema/intersection.d.ts +54 -0
- package/dist/schema/intersection.d.ts.map +1 -1
- package/dist/schema/intersection.js +49 -0
- package/dist/schema/intersection.js.map +1 -1
- package/dist/schema/literal.d.ts +44 -0
- package/dist/schema/literal.d.ts.map +1 -1
- package/dist/schema/literal.js +44 -0
- package/dist/schema/literal.js.map +1 -1
- package/dist/schema/never.d.ts +42 -0
- package/dist/schema/never.d.ts.map +1 -1
- package/dist/schema/never.js +42 -0
- package/dist/schema/never.js.map +1 -1
- package/dist/schema/null.d.ts +29 -0
- package/dist/schema/null.d.ts.map +1 -1
- package/dist/schema/null.js +29 -0
- package/dist/schema/null.js.map +1 -1
- package/dist/schema/nullable.d.ts +41 -0
- package/dist/schema/nullable.d.ts.map +1 -1
- package/dist/schema/nullable.js +41 -0
- package/dist/schema/nullable.js.map +1 -1
- package/dist/schema/object.d.ts +56 -0
- package/dist/schema/object.d.ts.map +1 -1
- package/dist/schema/object.js +51 -0
- package/dist/schema/object.js.map +1 -1
- package/dist/schema/optional.d.ts +42 -0
- package/dist/schema/optional.d.ts.map +1 -1
- package/dist/schema/optional.js +42 -0
- package/dist/schema/optional.js.map +1 -1
- package/dist/schema/params.d.ts +90 -10
- package/dist/schema/params.d.ts.map +1 -1
- package/dist/schema/params.js +84 -10
- package/dist/schema/params.js.map +1 -1
- package/dist/schema/payload.d.ts +111 -15
- package/dist/schema/payload.d.ts.map +1 -1
- package/dist/schema/payload.js +70 -0
- package/dist/schema/payload.js.map +1 -1
- package/dist/schema/permission-set.d.ts +58 -0
- package/dist/schema/permission-set.d.ts.map +1 -1
- package/dist/schema/permission-set.js +50 -0
- package/dist/schema/permission-set.js.map +1 -1
- package/dist/schema/permission.d.ts +42 -0
- package/dist/schema/permission.d.ts.map +1 -1
- package/dist/schema/permission.js +39 -0
- package/dist/schema/permission.js.map +1 -1
- package/dist/schema/procedure.d.ts +64 -0
- package/dist/schema/procedure.d.ts.map +1 -1
- package/dist/schema/procedure.js +64 -0
- package/dist/schema/procedure.js.map +1 -1
- package/dist/schema/query.d.ts +55 -0
- package/dist/schema/query.d.ts.map +1 -1
- package/dist/schema/query.js +55 -0
- package/dist/schema/query.js.map +1 -1
- package/dist/schema/record.d.ts +63 -8
- package/dist/schema/record.d.ts.map +1 -1
- package/dist/schema/record.js +20 -0
- package/dist/schema/record.js.map +1 -1
- package/dist/schema/ref.d.ts +50 -0
- package/dist/schema/ref.d.ts.map +1 -1
- package/dist/schema/ref.js +17 -0
- package/dist/schema/ref.js.map +1 -1
- package/dist/schema/refine.d.ts +58 -9
- package/dist/schema/refine.d.ts.map +1 -1
- package/dist/schema/refine.js.map +1 -1
- package/dist/schema/regexp.d.ts +44 -0
- package/dist/schema/regexp.d.ts.map +1 -1
- package/dist/schema/regexp.js +44 -0
- package/dist/schema/regexp.js.map +1 -1
- package/dist/schema/string.d.ts +50 -0
- package/dist/schema/string.d.ts.map +1 -1
- package/dist/schema/string.js +41 -0
- package/dist/schema/string.js.map +1 -1
- package/dist/schema/subscription.d.ts +72 -2
- package/dist/schema/subscription.d.ts.map +1 -1
- package/dist/schema/subscription.js +59 -0
- package/dist/schema/subscription.js.map +1 -1
- package/dist/schema/token.d.ts +47 -0
- package/dist/schema/token.d.ts.map +1 -1
- package/dist/schema/token.js +47 -0
- package/dist/schema/token.js.map +1 -1
- package/dist/schema/typed-object.d.ts +62 -8
- package/dist/schema/typed-object.d.ts.map +1 -1
- package/dist/schema/typed-object.js +18 -0
- package/dist/schema/typed-object.js.map +1 -1
- package/dist/schema/typed-ref.d.ts +53 -0
- package/dist/schema/typed-ref.d.ts.map +1 -1
- package/dist/schema/typed-ref.js +15 -0
- package/dist/schema/typed-ref.js.map +1 -1
- package/dist/schema/typed-union.d.ts +50 -1
- package/dist/schema/typed-union.d.ts.map +1 -1
- package/dist/schema/typed-union.js +50 -1
- package/dist/schema/typed-union.js.map +1 -1
- package/dist/schema/union.d.ts +45 -0
- package/dist/schema/union.d.ts.map +1 -1
- package/dist/schema/union.js +40 -0
- package/dist/schema/union.js.map +1 -1
- package/dist/schema/unknown-object.d.ts +34 -0
- package/dist/schema/unknown-object.d.ts.map +1 -1
- package/dist/schema/unknown-object.js +31 -0
- package/dist/schema/unknown-object.js.map +1 -1
- package/dist/schema/unknown.d.ts +33 -0
- package/dist/schema/unknown.d.ts.map +1 -1
- package/dist/schema/unknown.js +33 -0
- package/dist/schema/unknown.js.map +1 -1
- package/dist/schema/with-default.d.ts +44 -0
- package/dist/schema/with-default.d.ts.map +1 -1
- package/dist/schema/with-default.js +44 -0
- package/dist/schema/with-default.js.map +1 -1
- package/package.json +3 -3
- package/src/core/$type.ts +150 -18
- package/src/core/record-key.ts +44 -0
- package/src/core/result.ts +86 -4
- package/src/core/schema.ts +236 -7
- package/src/core/string-format.ts +259 -13
- package/src/core/types.ts +91 -3
- package/src/core/validation-error.ts +60 -0
- package/src/core/validation-issue.ts +65 -0
- package/src/core/validator.ts +351 -10
- package/src/helpers.test.ts +110 -29
- package/src/helpers.ts +9 -14
- package/src/schema/array.test.ts +94 -79
- package/src/schema/array.ts +45 -0
- package/src/schema/blob.ts +46 -0
- package/src/schema/boolean.ts +28 -0
- package/src/schema/bytes.ts +38 -0
- package/src/schema/cid.ts +38 -0
- package/src/schema/custom.ts +66 -1
- package/src/schema/dict.ts +44 -0
- package/src/schema/discriminated-union.ts +58 -0
- package/src/schema/enum.ts +48 -0
- package/src/schema/integer.ts +42 -0
- package/src/schema/intersection.ts +54 -0
- package/src/schema/literal.ts +44 -0
- package/src/schema/never.ts +42 -0
- package/src/schema/null.ts +29 -0
- package/src/schema/nullable.ts +41 -0
- package/src/schema/object.ts +56 -0
- package/src/schema/optional.ts +42 -0
- package/src/schema/params.test.ts +58 -2
- package/src/schema/params.ts +125 -19
- package/src/schema/payload.test.ts +3 -3
- package/src/schema/payload.ts +142 -38
- package/src/schema/permission-set.ts +58 -0
- package/src/schema/permission.ts +42 -0
- package/src/schema/procedure.ts +64 -0
- package/src/schema/query.ts +55 -0
- package/src/schema/record.ts +63 -8
- package/src/schema/ref.ts +50 -0
- package/src/schema/refine.ts +58 -9
- package/src/schema/regexp.ts +44 -0
- package/src/schema/string.ts +50 -0
- package/src/schema/subscription.ts +72 -2
- package/src/schema/token.ts +47 -0
- package/src/schema/typed-object.ts +62 -8
- package/src/schema/typed-ref.ts +53 -0
- package/src/schema/typed-union.ts +55 -2
- package/src/schema/union.ts +45 -0
- package/src/schema/unknown-object.ts +34 -0
- package/src/schema/unknown.ts +33 -0
- package/src/schema/with-default.ts +44 -0
package/src/schema/params.ts
CHANGED
|
@@ -10,31 +10,84 @@ import {
|
|
|
10
10
|
} from '../core.js'
|
|
11
11
|
import { lazyProperty } from '../util/lazy-property.js'
|
|
12
12
|
import { memoizedOptions } from '../util/memoize.js'
|
|
13
|
-
import { array } from './array.js'
|
|
14
|
-
import { boolean } from './boolean.js'
|
|
13
|
+
import { ArraySchema, array } from './array.js'
|
|
14
|
+
import { BooleanSchema, boolean } from './boolean.js'
|
|
15
15
|
import { dict } from './dict.js'
|
|
16
|
-
import { integer } from './integer.js'
|
|
17
|
-
import { optional } from './optional.js'
|
|
16
|
+
import { IntegerSchema, integer } from './integer.js'
|
|
17
|
+
import { OptionalSchema, optional } from './optional.js'
|
|
18
18
|
import { StringSchema, string } from './string.js'
|
|
19
19
|
import { union } from './union.js'
|
|
20
|
+
import { WithDefaultSchema } from './with-default.js'
|
|
20
21
|
|
|
22
|
+
/**
|
|
23
|
+
* Scalar types allowed in URL parameters: boolean, integer, or string.
|
|
24
|
+
*/
|
|
21
25
|
export type ParamScalar = Infer<typeof paramScalarSchema>
|
|
22
26
|
const paramScalarSchema = union([boolean(), integer(), string()])
|
|
23
27
|
|
|
28
|
+
/**
|
|
29
|
+
* A single parameter value: scalar or array of scalars.
|
|
30
|
+
*/
|
|
24
31
|
export type Param = Infer<typeof paramSchema>
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Schema for validating individual parameter values.
|
|
35
|
+
*/
|
|
25
36
|
export const paramSchema = union([paramScalarSchema, array(paramScalarSchema)])
|
|
26
37
|
|
|
38
|
+
/**
|
|
39
|
+
* Type for a params object with string keys and optional param values.
|
|
40
|
+
*/
|
|
27
41
|
export type Params = Infer<typeof paramsSchema>
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Schema for validating arbitrary params objects.
|
|
45
|
+
*/
|
|
28
46
|
export const paramsSchema = dict(string(), optional(paramSchema))
|
|
29
47
|
|
|
48
|
+
// @NOTE In order to properly coerce URLSearchParams, we need to distinguish
|
|
49
|
+
// between scalar and array validators, requiring to be able to detect which
|
|
50
|
+
// schema types are being used, restricting the allowed param validators here.
|
|
51
|
+
type ParamScalarValidator = StringSchema | BooleanSchema | IntegerSchema
|
|
52
|
+
type ParamValueValidator =
|
|
53
|
+
| ParamScalarValidator
|
|
54
|
+
| ArraySchema<ParamScalarValidator>
|
|
55
|
+
type ParamValidator =
|
|
56
|
+
| ParamValueValidator
|
|
57
|
+
| OptionalSchema<ParamValueValidator>
|
|
58
|
+
| OptionalSchema<WithDefaultSchema<ParamValueValidator>>
|
|
59
|
+
| WithDefaultSchema<ParamValueValidator>
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Type representing the shape of a params schema definition.
|
|
63
|
+
*
|
|
64
|
+
* Maps parameter names to their validators (must be Param or undefined).
|
|
65
|
+
*/
|
|
30
66
|
export type ParamsSchemaShape = {
|
|
31
|
-
[x: string]:
|
|
67
|
+
[x: string]: ParamValidator
|
|
32
68
|
}
|
|
33
69
|
|
|
70
|
+
/**
|
|
71
|
+
* Schema for validating URL query parameters in Lexicon endpoints.
|
|
72
|
+
*
|
|
73
|
+
* Params are the query string parameters passed to queries, procedures,
|
|
74
|
+
* and subscriptions. Values must be scalars (boolean, integer, string)
|
|
75
|
+
* or arrays of scalars, as they need to be serializable to URL format.
|
|
76
|
+
*
|
|
77
|
+
* Provides methods for converting to/from URLSearchParams.
|
|
78
|
+
*
|
|
79
|
+
* @template TShape - The params shape type mapping names to validators
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```ts
|
|
83
|
+
* const schema = new ParamsSchema({
|
|
84
|
+
* limit: l.optional(l.integer({ minimum: 1, maximum: 100 })),
|
|
85
|
+
* cursor: l.optional(l.string()),
|
|
86
|
+
* })
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
34
89
|
export class ParamsSchema<
|
|
35
|
-
const TShape extends ParamsSchemaShape =
|
|
36
|
-
[x: string]: Validator<Param | undefined>
|
|
37
|
-
},
|
|
90
|
+
const TShape extends ParamsSchemaShape = ParamsSchemaShape,
|
|
38
91
|
> extends Schema<
|
|
39
92
|
WithOptionalProperties<{
|
|
40
93
|
[K in keyof TShape]: InferInput<TShape[K]>
|
|
@@ -47,14 +100,13 @@ export class ParamsSchema<
|
|
|
47
100
|
super()
|
|
48
101
|
}
|
|
49
102
|
|
|
50
|
-
get shapeValidators(): Map<string,
|
|
103
|
+
get shapeValidators(): Map<string, ParamValidator> {
|
|
51
104
|
const map = new Map(Object.entries(this.shape))
|
|
52
105
|
|
|
53
106
|
return lazyProperty(this, 'shapeValidators', map)
|
|
54
107
|
}
|
|
55
108
|
|
|
56
109
|
validateInContext(input: unknown, ctx: ValidationContext) {
|
|
57
|
-
// @TODO BETTER SUPPORT Input/Output
|
|
58
110
|
if (!isPlainObject(input)) {
|
|
59
111
|
return ctx.issueInvalidType(input, 'object')
|
|
60
112
|
}
|
|
@@ -111,14 +163,22 @@ export class ParamsSchema<
|
|
|
111
163
|
return ctx.success(copy ?? input)
|
|
112
164
|
}
|
|
113
165
|
|
|
114
|
-
fromURLSearchParams(
|
|
166
|
+
fromURLSearchParams(iterable: Iterable<[string, string]>): InferOutput<this> {
|
|
115
167
|
const params: Record<string, Param> = {}
|
|
116
168
|
|
|
117
|
-
|
|
118
|
-
|
|
169
|
+
// Compatibility with URLSearchParams not being iterable in some environments
|
|
170
|
+
const entries =
|
|
171
|
+
iterable instanceof URLSearchParams ? iterable.entries() : iterable
|
|
172
|
+
|
|
173
|
+
for (const [key, value] of entries) {
|
|
174
|
+
const validator = unwrapValidator(this.shapeValidators.get(key))
|
|
175
|
+
const expectsArray = validator instanceof ArraySchema
|
|
176
|
+
const scalarValidator = expectsArray
|
|
177
|
+
? unwrapValidator(validator.validator)
|
|
178
|
+
: validator
|
|
119
179
|
|
|
120
180
|
const coerced: ParamScalar =
|
|
121
|
-
|
|
181
|
+
scalarValidator instanceof StringSchema
|
|
122
182
|
? value
|
|
123
183
|
: value === 'true'
|
|
124
184
|
? true
|
|
@@ -128,12 +188,13 @@ export class ParamsSchema<
|
|
|
128
188
|
? Number(value)
|
|
129
189
|
: value
|
|
130
190
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
191
|
+
const currentParam = params[key]
|
|
192
|
+
if (currentParam === undefined) {
|
|
193
|
+
params[key] = expectsArray ? [coerced] : coerced
|
|
194
|
+
} else if (Array.isArray(currentParam)) {
|
|
195
|
+
currentParam.push(coerced)
|
|
135
196
|
} else {
|
|
136
|
-
params[key] = [
|
|
197
|
+
params[key] = [currentParam, coerced]
|
|
137
198
|
}
|
|
138
199
|
}
|
|
139
200
|
|
|
@@ -161,8 +222,53 @@ export class ParamsSchema<
|
|
|
161
222
|
}
|
|
162
223
|
}
|
|
163
224
|
|
|
225
|
+
/**
|
|
226
|
+
* Creates a params schema for URL query parameters.
|
|
227
|
+
*
|
|
228
|
+
* Params schemas validate query string parameters for Lexicon endpoints.
|
|
229
|
+
* Values must be boolean, integer, string, or arrays of those types.
|
|
230
|
+
*
|
|
231
|
+
* @param properties - Object mapping parameter names to their validators
|
|
232
|
+
* @returns A new {@link ParamsSchema} instance
|
|
233
|
+
*
|
|
234
|
+
* @example
|
|
235
|
+
* ```ts
|
|
236
|
+
* // Simple pagination params
|
|
237
|
+
* const paginationParams = l.params({
|
|
238
|
+
* limit: l.optional(l.withDefault(l.integer({ minimum: 1, maximum: 100 }), 50)),
|
|
239
|
+
* cursor: l.optional(l.string()),
|
|
240
|
+
* })
|
|
241
|
+
*
|
|
242
|
+
* // Required parameter
|
|
243
|
+
* const actorParams = l.params({
|
|
244
|
+
* actor: l.string({ format: 'at-identifier' }),
|
|
245
|
+
* })
|
|
246
|
+
*
|
|
247
|
+
* // Array parameter (multiple values)
|
|
248
|
+
* const filterParams = l.params({
|
|
249
|
+
* tags: l.optional(l.array(l.string())),
|
|
250
|
+
* })
|
|
251
|
+
*
|
|
252
|
+
* // Convert from URL
|
|
253
|
+
* const urlParams = new URLSearchParams('limit=25&cursor=abc')
|
|
254
|
+
* const validated = paginationParams.fromURLSearchParams(urlParams)
|
|
255
|
+
*
|
|
256
|
+
* // Convert to URL
|
|
257
|
+
* const searchParams = paginationParams.toURLSearchParams({ limit: 25 })
|
|
258
|
+
* ```
|
|
259
|
+
*/
|
|
164
260
|
export const params = /*#__PURE__*/ memoizedOptions(function params<
|
|
165
261
|
const TShape extends ParamsSchemaShape = NonNullable<unknown>,
|
|
166
262
|
>(properties: TShape = {} as TShape) {
|
|
167
263
|
return new ParamsSchema<TShape>(properties)
|
|
168
264
|
})
|
|
265
|
+
|
|
266
|
+
function unwrapValidator(schema?: Validator): Validator | undefined {
|
|
267
|
+
while (
|
|
268
|
+
schema instanceof OptionalSchema ||
|
|
269
|
+
schema instanceof WithDefaultSchema
|
|
270
|
+
) {
|
|
271
|
+
schema = schema.validator
|
|
272
|
+
}
|
|
273
|
+
return schema
|
|
274
|
+
}
|
|
@@ -3,7 +3,7 @@ import { integer } from './integer.js'
|
|
|
3
3
|
import { object } from './object.js'
|
|
4
4
|
import { payload } from './payload.js'
|
|
5
5
|
import { string } from './string.js'
|
|
6
|
-
import {
|
|
6
|
+
import { unknownObject } from './unknown-object.js'
|
|
7
7
|
|
|
8
8
|
describe('Payload', () => {
|
|
9
9
|
describe('basic construction', () => {
|
|
@@ -113,7 +113,7 @@ describe('Payload', () => {
|
|
|
113
113
|
})
|
|
114
114
|
|
|
115
115
|
it('creates payload with unknown schema', () => {
|
|
116
|
-
const schema =
|
|
116
|
+
const schema = object({})
|
|
117
117
|
const def = payload('application/json', schema)
|
|
118
118
|
expect(def.encoding).toBe('application/json')
|
|
119
119
|
expect(def.schema).toBe(schema)
|
|
@@ -224,7 +224,7 @@ describe('Payload', () => {
|
|
|
224
224
|
'application/json',
|
|
225
225
|
object({
|
|
226
226
|
success: string(),
|
|
227
|
-
data:
|
|
227
|
+
data: unknownObject(),
|
|
228
228
|
}),
|
|
229
229
|
)
|
|
230
230
|
expect(def.encoding).toBe('application/json')
|
package/src/schema/payload.ts
CHANGED
|
@@ -1,50 +1,101 @@
|
|
|
1
1
|
import { LexValue } from '@atproto/lex-data'
|
|
2
|
-
import { Infer, Schema } from '../core.js'
|
|
3
|
-
import { ObjectSchema,
|
|
4
|
-
|
|
5
|
-
export type
|
|
6
|
-
TPayload extends Payload,
|
|
7
|
-
TBody,
|
|
8
|
-
> = TPayload['encoding'] extends infer E extends string
|
|
9
|
-
? {
|
|
10
|
-
encoding: SchemaEncodingToDataEncoding<E>
|
|
11
|
-
body: InferPayloadBody<TPayload, TBody>
|
|
12
|
-
}
|
|
13
|
-
: void | undefined
|
|
2
|
+
import { Infer, Schema, Validator } from '../core.js'
|
|
3
|
+
import { ObjectSchema, object } from './object.js'
|
|
4
|
+
|
|
5
|
+
export type { LexValue }
|
|
14
6
|
|
|
15
|
-
|
|
7
|
+
type ToBodyMime<TEncoding extends string> = TEncoding extends '*/*'
|
|
16
8
|
? `${string}/${string}`
|
|
17
|
-
:
|
|
9
|
+
: TEncoding extends `${infer T extends string}/*`
|
|
18
10
|
? `${T}/${string}`
|
|
19
|
-
:
|
|
11
|
+
: TEncoding
|
|
12
|
+
|
|
13
|
+
type ToBodyType<
|
|
14
|
+
TEncoding extends string,
|
|
15
|
+
TSchema,
|
|
16
|
+
TBinary,
|
|
17
|
+
> = TSchema extends Schema
|
|
18
|
+
? Infer<TSchema>
|
|
19
|
+
: TEncoding extends `application/json`
|
|
20
|
+
? LexValue
|
|
21
|
+
: TBinary
|
|
20
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Infers the type of a Payload's encoding and body.
|
|
25
|
+
*
|
|
26
|
+
* @template TPayload - The Payload type
|
|
27
|
+
* @template TBody - Fallback body type for non-JSON encodings
|
|
28
|
+
*/
|
|
29
|
+
export type InferPayload<TPayload extends Payload, TBinary> =
|
|
30
|
+
TPayload extends Payload<infer TEncoding, infer TSchema>
|
|
31
|
+
? TEncoding extends string
|
|
32
|
+
? {
|
|
33
|
+
encoding: ToBodyMime<TEncoding>
|
|
34
|
+
body: ToBodyType<TEncoding, TSchema, TBinary>
|
|
35
|
+
}
|
|
36
|
+
: undefined
|
|
37
|
+
: never
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Converts schema encoding patterns to data encoding types.
|
|
41
|
+
*
|
|
42
|
+
* Handles wildcards like '*\/*' and 'image/*' in MIME types.
|
|
43
|
+
*
|
|
44
|
+
* @template TPayload - The Payload type
|
|
45
|
+
*/
|
|
21
46
|
export type InferPayloadEncoding<TPayload extends Payload> =
|
|
22
|
-
TPayload
|
|
23
|
-
?
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
47
|
+
TPayload extends Payload<infer TEncoding, any>
|
|
48
|
+
? TEncoding extends string
|
|
49
|
+
? ToBodyMime<TEncoding>
|
|
50
|
+
: undefined
|
|
51
|
+
: never
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Infers the body type from a Payload and fallback type.
|
|
55
|
+
*
|
|
56
|
+
* @template TPayload - The Payload type
|
|
57
|
+
* @template TBody - Fallback body type for non-JSON encodings without schema
|
|
58
|
+
*/
|
|
59
|
+
export type InferPayloadBody<TPayload extends Payload, TBinary> =
|
|
60
|
+
TPayload extends Payload<infer TEncoding, infer TSchema>
|
|
61
|
+
? TEncoding extends string
|
|
62
|
+
? ToBodyType<TEncoding, TSchema, TBinary>
|
|
63
|
+
: undefined
|
|
64
|
+
: never
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Determines valid schema type based on encoding presence.
|
|
68
|
+
*
|
|
69
|
+
* @template E - The encoding string type, or undefined
|
|
70
|
+
*/
|
|
71
|
+
export type PayloadSchema<E extends string | undefined> = E extends undefined
|
|
38
72
|
? undefined
|
|
39
|
-
: Schema | undefined
|
|
73
|
+
: Schema<LexValue> | undefined
|
|
40
74
|
|
|
75
|
+
/**
|
|
76
|
+
* Represents a payload definition for Lexicon endpoints.
|
|
77
|
+
*
|
|
78
|
+
* Payloads define the body format for HTTP requests and responses.
|
|
79
|
+
* They consist of an encoding (MIME type) and an optional schema
|
|
80
|
+
* for validating the body content.
|
|
81
|
+
*
|
|
82
|
+
* @template TEncoding - The MIME type string, or undefined for no body
|
|
83
|
+
* @template TPayload - The schema type for body validation
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```ts
|
|
87
|
+
* const jsonPayload = new Payload('application/json', l.object({ data: l.string() }))
|
|
88
|
+
* const binaryPayload = new Payload('image/*', undefined)
|
|
89
|
+
* const noPayload = new Payload(undefined, undefined)
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
41
92
|
export class Payload<
|
|
42
93
|
const TEncoding extends string | undefined = string | undefined,
|
|
43
|
-
const
|
|
94
|
+
const TSchema extends PayloadSchema<TEncoding> = PayloadSchema<TEncoding>,
|
|
44
95
|
> {
|
|
45
96
|
constructor(
|
|
46
97
|
readonly encoding: TEncoding,
|
|
47
|
-
readonly schema:
|
|
98
|
+
readonly schema: TSchema,
|
|
48
99
|
) {
|
|
49
100
|
if (encoding === undefined && schema !== undefined) {
|
|
50
101
|
throw new TypeError('schema cannot be defined when encoding is undefined')
|
|
@@ -86,17 +137,70 @@ export class Payload<
|
|
|
86
137
|
}
|
|
87
138
|
}
|
|
88
139
|
|
|
140
|
+
/**
|
|
141
|
+
* Creates a payload definition for Lexicon endpoint bodies.
|
|
142
|
+
*
|
|
143
|
+
* Defines the expected MIME type and optional validation schema for
|
|
144
|
+
* request or response bodies.
|
|
145
|
+
*
|
|
146
|
+
* @param encoding - MIME type string (e.g., 'application/json', 'image/*'), or undefined for no body
|
|
147
|
+
* @param validator - Optional schema for validating the body content. Must be undefined if encoding is undefined.
|
|
148
|
+
* @returns A new {@link Payload} instance
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```ts
|
|
152
|
+
* // JSON payload with schema
|
|
153
|
+
* const output = l.payload('application/json', l.object({
|
|
154
|
+
* posts: l.array(postSchema),
|
|
155
|
+
* cursor: l.optional(l.string()),
|
|
156
|
+
* }))
|
|
157
|
+
*
|
|
158
|
+
* // Binary payload (no schema validation)
|
|
159
|
+
* const blobInput = l.payload('*\/*', undefined)
|
|
160
|
+
*
|
|
161
|
+
* // Image payload with wildcard
|
|
162
|
+
* const imageInput = l.payload('image/*', undefined)
|
|
163
|
+
*
|
|
164
|
+
* // No payload (for endpoints without body)
|
|
165
|
+
* const noBody = l.payload()
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
89
168
|
/*@__NO_SIDE_EFFECTS__*/
|
|
90
169
|
export function payload<
|
|
91
170
|
const E extends string | undefined = undefined,
|
|
92
|
-
const S extends
|
|
171
|
+
const S extends PayloadSchema<E> = undefined,
|
|
93
172
|
>(encoding: E = undefined as E, validator: S = undefined as S) {
|
|
94
173
|
return new Payload<E, S>(encoding, validator)
|
|
95
174
|
}
|
|
96
175
|
|
|
176
|
+
/**
|
|
177
|
+
* Creates a JSON payload with an object schema.
|
|
178
|
+
*
|
|
179
|
+
* Convenience function for the common case of JSON request/response bodies.
|
|
180
|
+
* Equivalent to `l.payload('application/json', l.object(properties))`.
|
|
181
|
+
*
|
|
182
|
+
* @param properties - Object mapping property names to validators
|
|
183
|
+
* @returns A new {@link Payload} instance with 'application/json' encoding
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* ```ts
|
|
187
|
+
* // Query output
|
|
188
|
+
* const profileOutput = l.jsonPayload({
|
|
189
|
+
* did: l.string({ format: 'did' }),
|
|
190
|
+
* handle: l.string({ format: 'handle' }),
|
|
191
|
+
* displayName: l.optional(l.string()),
|
|
192
|
+
* })
|
|
193
|
+
*
|
|
194
|
+
* // Procedure input
|
|
195
|
+
* const createPostInput = l.jsonPayload({
|
|
196
|
+
* text: l.string({ maxGraphemes: 300 }),
|
|
197
|
+
* createdAt: l.string({ format: 'datetime' }),
|
|
198
|
+
* })
|
|
199
|
+
* ```
|
|
200
|
+
*/
|
|
97
201
|
/*@__NO_SIDE_EFFECTS__*/
|
|
98
|
-
export function jsonPayload<
|
|
99
|
-
|
|
100
|
-
): Payload<'application/json', ObjectSchema<P>> {
|
|
202
|
+
export function jsonPayload<
|
|
203
|
+
P extends Record<string, Validator<undefined | LexValue>>,
|
|
204
|
+
>(properties: P): Payload<'application/json', ObjectSchema<P>> {
|
|
101
205
|
return payload('application/json', object(properties))
|
|
102
206
|
}
|
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
import { NsidString } from '../core.js'
|
|
2
2
|
import { Permission } from './permission.js'
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Configuration options for a permission set.
|
|
6
|
+
*
|
|
7
|
+
* @property title - Human-readable title for the permission set
|
|
8
|
+
* @property title:lang - Localized titles by language code
|
|
9
|
+
* @property detail - Detailed description of the permission set
|
|
10
|
+
* @property detail:lang - Localized descriptions by language code
|
|
11
|
+
*/
|
|
4
12
|
export type PermissionSetOptions = {
|
|
5
13
|
title?: string
|
|
6
14
|
'title:lang'?: Record<string, undefined | string>
|
|
@@ -8,6 +16,24 @@ export type PermissionSetOptions = {
|
|
|
8
16
|
'detail:lang'?: Record<string, undefined | string>
|
|
9
17
|
}
|
|
10
18
|
|
|
19
|
+
/**
|
|
20
|
+
* Represents a collection of related permissions in AT Protocol.
|
|
21
|
+
*
|
|
22
|
+
* Permission sets group permissions together with metadata for OAuth
|
|
23
|
+
* authorization flows. They are identified by an NSID.
|
|
24
|
+
*
|
|
25
|
+
* @template TNsid - The NSID identifying this permission set
|
|
26
|
+
* @template TPermissions - Tuple type of the included permissions
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* const feedAccess = new PermissionSet(
|
|
31
|
+
* 'app.bsky.feed.access',
|
|
32
|
+
* [readPermission, writePermission],
|
|
33
|
+
* { title: 'Feed Access', detail: 'Read and write to your feed' }
|
|
34
|
+
* )
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
11
37
|
export class PermissionSet<
|
|
12
38
|
const TNsid extends NsidString = any,
|
|
13
39
|
const TPermissions extends readonly Permission[] = any,
|
|
@@ -19,6 +45,38 @@ export class PermissionSet<
|
|
|
19
45
|
) {}
|
|
20
46
|
}
|
|
21
47
|
|
|
48
|
+
/**
|
|
49
|
+
* Creates a permission set grouping related permissions.
|
|
50
|
+
*
|
|
51
|
+
* Permission sets define OAuth scopes that applications can request.
|
|
52
|
+
* They include human-readable metadata for authorization UIs.
|
|
53
|
+
*
|
|
54
|
+
* @param nsid - The NSID identifying this permission set
|
|
55
|
+
* @param permissions - Array of permissions included in this set
|
|
56
|
+
* @param options - Optional metadata (title, detail, localization)
|
|
57
|
+
* @returns A new {@link PermissionSet} instance
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* // Define individual permissions
|
|
62
|
+
* const readPosts = l.permission('read', { collection: 'app.bsky.feed.post' })
|
|
63
|
+
* const writePosts = l.permission('write', { collection: 'app.bsky.feed.post' })
|
|
64
|
+
*
|
|
65
|
+
* // Group into a permission set
|
|
66
|
+
* const postManagement = l.permissionSet(
|
|
67
|
+
* 'app.bsky.feed.postManagement',
|
|
68
|
+
* [readPosts, writePosts],
|
|
69
|
+
* {
|
|
70
|
+
* title: 'Post Management',
|
|
71
|
+
* detail: 'View and create posts on your behalf',
|
|
72
|
+
* 'title:lang': {
|
|
73
|
+
* 'es': 'Gestion de publicaciones',
|
|
74
|
+
* 'fr': 'Gestion des publications',
|
|
75
|
+
* },
|
|
76
|
+
* }
|
|
77
|
+
* )
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
22
80
|
/*@__NO_SIDE_EFFECTS__*/
|
|
23
81
|
export function permissionSet<
|
|
24
82
|
const N extends NsidString,
|
package/src/schema/permission.ts
CHANGED
|
@@ -1,7 +1,24 @@
|
|
|
1
1
|
import { Params } from './params.js'
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Type alias for permission options (same as Params).
|
|
5
|
+
*/
|
|
3
6
|
export type PermissionOptions = Params
|
|
4
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Represents a single permission in an AT Protocol permission set.
|
|
10
|
+
*
|
|
11
|
+
* Permissions define access rights to specific resources with optional
|
|
12
|
+
* parameters for fine-grained control.
|
|
13
|
+
*
|
|
14
|
+
* @template TResource - The resource identifier string type
|
|
15
|
+
* @template TOptions - The options type (must be valid Params)
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* const readPermission = new Permission('read', { collection: 'app.bsky.feed.post' })
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
5
22
|
export class Permission<
|
|
6
23
|
const TResource extends string = any,
|
|
7
24
|
const TOptions extends PermissionOptions = any,
|
|
@@ -12,6 +29,31 @@ export class Permission<
|
|
|
12
29
|
) {}
|
|
13
30
|
}
|
|
14
31
|
|
|
32
|
+
/**
|
|
33
|
+
* Creates a permission definition for AT Protocol authorization.
|
|
34
|
+
*
|
|
35
|
+
* Permissions specify what resources an application can access.
|
|
36
|
+
* Used in permission sets to define OAuth scopes.
|
|
37
|
+
*
|
|
38
|
+
* @param resource - The resource identifier (e.g., 'read', 'write', 'admin')
|
|
39
|
+
* @param options - Optional parameters for the permission
|
|
40
|
+
* @returns A new {@link Permission} instance
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* // Simple permission
|
|
45
|
+
* const readPermission = l.permission('read')
|
|
46
|
+
*
|
|
47
|
+
* // Permission with options
|
|
48
|
+
* const writePostsPermission = l.permission('write', {
|
|
49
|
+
* collection: 'app.bsky.feed.post',
|
|
50
|
+
* })
|
|
51
|
+
*
|
|
52
|
+
* // Multiple permissions with different scopes
|
|
53
|
+
* const readProfile = l.permission('read', { collection: 'app.bsky.actor.profile' })
|
|
54
|
+
* const readFeed = l.permission('read', { collection: 'app.bsky.feed.*' })
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
15
57
|
/*@__NO_SIDE_EFFECTS__*/
|
|
16
58
|
export function permission<
|
|
17
59
|
const R extends string,
|
package/src/schema/procedure.ts
CHANGED
|
@@ -2,6 +2,30 @@ import { NsidString } from '../core.js'
|
|
|
2
2
|
import { ParamsSchema } from './params.js'
|
|
3
3
|
import { Payload } from './payload.js'
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Represents a Lexicon procedure (HTTP POST) endpoint definition.
|
|
7
|
+
*
|
|
8
|
+
* Procedures are operations that may modify state on the server.
|
|
9
|
+
* They have parameters, an input payload (request body), an output
|
|
10
|
+
* payload (response body), and optional error types.
|
|
11
|
+
*
|
|
12
|
+
* @template TNsid - The NSID identifying this procedure
|
|
13
|
+
* @template TParameters - The parameters schema type
|
|
14
|
+
* @template TInputPayload - The request body payload type
|
|
15
|
+
* @template TOutputPayload - The response body payload type
|
|
16
|
+
* @template TErrors - Array of error type strings, or undefined
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* const createPost = new Procedure(
|
|
21
|
+
* 'app.bsky.feed.post',
|
|
22
|
+
* l.params({}),
|
|
23
|
+
* l.jsonPayload({ text: l.string() }),
|
|
24
|
+
* l.jsonPayload({ uri: l.string(), cid: l.string() }),
|
|
25
|
+
* ['InvalidRecord']
|
|
26
|
+
* )
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
5
29
|
export class Procedure<
|
|
6
30
|
const TNsid extends NsidString = NsidString,
|
|
7
31
|
const TParameters extends ParamsSchema = ParamsSchema,
|
|
@@ -22,6 +46,46 @@ export class Procedure<
|
|
|
22
46
|
) {}
|
|
23
47
|
}
|
|
24
48
|
|
|
49
|
+
/**
|
|
50
|
+
* Creates a procedure definition for a Lexicon POST endpoint.
|
|
51
|
+
*
|
|
52
|
+
* Procedures can modify server state. They accept both URL parameters
|
|
53
|
+
* and a request body (input payload).
|
|
54
|
+
*
|
|
55
|
+
* @param nsid - The NSID identifying this procedure endpoint
|
|
56
|
+
* @param parameters - Schema for URL query parameters
|
|
57
|
+
* @param input - Schema for request body payload
|
|
58
|
+
* @param output - Schema for response body payload
|
|
59
|
+
* @param errors - Optional array of error type strings
|
|
60
|
+
* @returns A new {@link Procedure} instance
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* // Create record procedure
|
|
65
|
+
* const createRecord = l.procedure(
|
|
66
|
+
* 'com.atproto.repo.createRecord',
|
|
67
|
+
* l.params({}),
|
|
68
|
+
* l.jsonPayload({
|
|
69
|
+
* repo: l.string({ format: 'at-identifier' }),
|
|
70
|
+
* collection: l.string({ format: 'nsid' }),
|
|
71
|
+
* record: l.unknown(),
|
|
72
|
+
* }),
|
|
73
|
+
* l.jsonPayload({
|
|
74
|
+
* uri: l.string({ format: 'at-uri' }),
|
|
75
|
+
* cid: l.string({ format: 'cid' }),
|
|
76
|
+
* }),
|
|
77
|
+
* ['InvalidRecord', 'RepoNotFound'],
|
|
78
|
+
* )
|
|
79
|
+
*
|
|
80
|
+
* // Procedure with binary input
|
|
81
|
+
* const uploadBlob = l.procedure(
|
|
82
|
+
* 'com.atproto.repo.uploadBlob',
|
|
83
|
+
* l.params({}),
|
|
84
|
+
* l.payload('*\/*', undefined), // Accept any content type
|
|
85
|
+
* l.jsonPayload({ blob: l.blob() }),
|
|
86
|
+
* )
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
25
89
|
/*@__NO_SIDE_EFFECTS__*/
|
|
26
90
|
export function procedure<
|
|
27
91
|
const N extends NsidString,
|