@sapporta/rest-core 3.52.1 → 3.52.2
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 +6 -0
- package/README.md +75 -10
- package/index.cjs.d.ts +1 -0
- package/index.cjs.default.js +1 -0
- package/index.cjs.js +807 -0
- package/index.cjs.mjs +2 -0
- package/index.esm.js +762 -0
- package/package.json +13 -3
- package/src/lib/client.d.ts +107 -0
- package/src/lib/dsl.d.ts +222 -0
- package/src/lib/infer-types.d.ts +78 -0
- package/src/lib/paths.d.ts +30 -0
- package/src/lib/query.d.ts +17 -0
- package/src/lib/response-error.d.ts +20 -0
- package/src/lib/response-validation-error.d.ts +14 -0
- package/src/lib/server.d.ts +18 -0
- package/src/lib/standard-schema-utils.d.ts +68 -0
- package/src/lib/standard-schema.d.ts +55 -0
- package/src/lib/status-codes.d.ts +6 -0
- package/src/lib/{test-helpers.ts → test-helpers.d.ts} +1 -6
- package/src/lib/type-guards.d.ts +12 -0
- package/src/lib/type-utils.d.ts +96 -0
- package/src/lib/unknown-status-error.d.ts +10 -0
- package/src/lib/validation-error.d.ts +11 -0
- package/.babelrc +0 -10
- package/.eslintrc.json +0 -21
- package/LICENCE +0 -21
- package/jest.config.ts +0 -16
- package/project.json +0 -51
- package/src/lib/client.spec.ts +0 -1330
- package/src/lib/client.ts +0 -481
- package/src/lib/dsl.spec.ts +0 -1308
- package/src/lib/dsl.ts +0 -472
- package/src/lib/fetch.spec.ts +0 -102
- package/src/lib/infer-types.spec.ts +0 -935
- package/src/lib/infer-types.ts +0 -282
- package/src/lib/paths.spec.ts +0 -138
- package/src/lib/paths.ts +0 -61
- package/src/lib/query.spec.ts +0 -329
- package/src/lib/query.ts +0 -114
- package/src/lib/response-error.spec.ts +0 -67
- package/src/lib/response-error.ts +0 -61
- package/src/lib/response-validation-error.ts +0 -24
- package/src/lib/server.spec.ts +0 -163
- package/src/lib/server.ts +0 -83
- package/src/lib/standard-schema-utils.spec.ts +0 -218
- package/src/lib/standard-schema-utils.ts +0 -280
- package/src/lib/standard-schema.ts +0 -71
- package/src/lib/status-codes.ts +0 -75
- package/src/lib/type-guards.spec.ts +0 -355
- package/src/lib/type-guards.ts +0 -99
- package/src/lib/type-utils.spec.ts +0 -59
- package/src/lib/type-utils.ts +0 -234
- package/src/lib/unknown-status-error.ts +0 -15
- package/src/lib/validation-error.ts +0 -36
- package/tsconfig.json +0 -22
- package/tsconfig.lib.json +0 -10
- package/tsconfig.spec.json +0 -9
- package/typedoc.json +0 -5
- /package/src/{index.ts → index.d.ts} +0 -0
|
@@ -1,280 +0,0 @@
|
|
|
1
|
-
import { ZodError } from 'zod';
|
|
2
|
-
import { StandardSchemaV1 } from './standard-schema';
|
|
3
|
-
import { StandardSchemaError } from './validation-error';
|
|
4
|
-
import { ContractAnyType } from './dsl';
|
|
5
|
-
|
|
6
|
-
const VENDOR_STANDARD_SCHEMA = 'ts-rest-combined';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Type guard to check if the schema is a standard schema.
|
|
10
|
-
*
|
|
11
|
-
* @param schema - unknown
|
|
12
|
-
* @returns boolean
|
|
13
|
-
*/
|
|
14
|
-
export const isStandardSchema = (
|
|
15
|
-
schema: unknown,
|
|
16
|
-
): schema is StandardSchemaV1 => {
|
|
17
|
-
if (!schema) {
|
|
18
|
-
return false;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const standard = (schema as StandardSchemaV1)?.['~standard'];
|
|
22
|
-
|
|
23
|
-
if (!standard) {
|
|
24
|
-
return false;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return standard.version === 1 && typeof standard.validate === 'function';
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Takes in an unknown object and returns either a standard schema or null
|
|
32
|
-
*
|
|
33
|
-
* @param schema - unknown
|
|
34
|
-
* @returns StandardSchemaV1<unknown, unknown> | null
|
|
35
|
-
*/
|
|
36
|
-
export const parseAsStandardSchema = (
|
|
37
|
-
schema: unknown,
|
|
38
|
-
): StandardSchemaV1<unknown, unknown> | null => {
|
|
39
|
-
const isStandard = isStandardSchema(schema);
|
|
40
|
-
|
|
41
|
-
if (isStandard) {
|
|
42
|
-
return schema;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return null;
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Since 3.53.0 we've moved to headers using an object with schemas inside it, rather than a top level schema.
|
|
50
|
-
*
|
|
51
|
-
* This makes it easier to merge schemas together.
|
|
52
|
-
*
|
|
53
|
-
* @param data - Data to validate e.g. headers
|
|
54
|
-
* @param schemaObject - Schema object to validate against e.g. { 'x-foo': v.string() }
|
|
55
|
-
* @returns
|
|
56
|
-
*/
|
|
57
|
-
export const validateMultiSchemaObject = (
|
|
58
|
-
data: unknown,
|
|
59
|
-
schemaObject: Record<string, ContractAnyType> | undefined,
|
|
60
|
-
): {
|
|
61
|
-
value?: unknown;
|
|
62
|
-
error?: StandardSchemaError | ZodError;
|
|
63
|
-
schemasUsed: Array<StandardSchemaV1<unknown, unknown>>;
|
|
64
|
-
} => {
|
|
65
|
-
const schema = parseAsStandardSchema(schemaObject);
|
|
66
|
-
|
|
67
|
-
// If the top level is not null we know it's a valid schema we can validate against
|
|
68
|
-
if (schema !== null) {
|
|
69
|
-
const result = validateAgainstStandardSchema(data, schema, {
|
|
70
|
-
passThroughExtraKeys: true,
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
return {
|
|
74
|
-
value: result.value,
|
|
75
|
-
error: result.error,
|
|
76
|
-
schemasUsed: [schema],
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const headersMap = new Map<string, unknown>(Object.entries(data ?? {}));
|
|
81
|
-
|
|
82
|
-
const vendorSet = new Set<string>();
|
|
83
|
-
const subSchemas = new Map<string, StandardSchemaV1<unknown, unknown>>();
|
|
84
|
-
|
|
85
|
-
for (const [key, schema] of Object.entries(schemaObject ?? {})) {
|
|
86
|
-
const parsedSchema = parseAsStandardSchema(schema);
|
|
87
|
-
|
|
88
|
-
if (schema === null) {
|
|
89
|
-
continue;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
if (typeof schema === 'symbol') {
|
|
93
|
-
continue;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (parsedSchema !== null) {
|
|
97
|
-
subSchemas.set(key, parsedSchema);
|
|
98
|
-
vendorSet.add(parsedSchema['~standard'].vendor);
|
|
99
|
-
} else {
|
|
100
|
-
throw new Error(
|
|
101
|
-
`Invalid schema provided for header ${key}, please use a valid schema`,
|
|
102
|
-
);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
if (subSchemas.size === 0) {
|
|
107
|
-
return {
|
|
108
|
-
value: data,
|
|
109
|
-
schemasUsed: [],
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
const issues: StandardSchemaV1.Issue[] = [];
|
|
114
|
-
|
|
115
|
-
for (const [key, schema] of subSchemas.entries()) {
|
|
116
|
-
const value = headersMap.get(key);
|
|
117
|
-
const result = validateAgainstStandardSchema(value, schema);
|
|
118
|
-
|
|
119
|
-
if (result.error) {
|
|
120
|
-
for (const issue of result.error.issues) {
|
|
121
|
-
issues.push({
|
|
122
|
-
...issue,
|
|
123
|
-
path: [key],
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
} else {
|
|
127
|
-
headersMap.set(key, result.value);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
if (issues.length > 0) {
|
|
132
|
-
return {
|
|
133
|
-
error: new StandardSchemaError(issues),
|
|
134
|
-
schemasUsed: [...subSchemas.values()],
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
return {
|
|
139
|
-
value: Object.fromEntries(headersMap.entries()),
|
|
140
|
-
schemasUsed: [...subSchemas.values()],
|
|
141
|
-
};
|
|
142
|
-
};
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Combines two standard schemas into a single standard schema.
|
|
146
|
-
*
|
|
147
|
-
* The combined schema will run the validation of both schemas and return the result of the first schema that
|
|
148
|
-
* succeeds.
|
|
149
|
-
*
|
|
150
|
-
* If either schema fails, the combined schema will return the issues from both schemas.
|
|
151
|
-
*
|
|
152
|
-
* @param a - StandardSchemaV1<unknown, unknown>
|
|
153
|
-
* @param b - StandardSchemaV1<unknown, unknown>
|
|
154
|
-
* @returns StandardSchemaV1<unknown, unknown>
|
|
155
|
-
*/
|
|
156
|
-
export const combineStandardSchemas = (
|
|
157
|
-
a: StandardSchemaV1<unknown, unknown>,
|
|
158
|
-
b: StandardSchemaV1<unknown, unknown>,
|
|
159
|
-
): StandardSchemaV1<unknown, unknown> => {
|
|
160
|
-
const standardSchema: StandardSchemaV1<unknown, unknown>['~standard'] = {
|
|
161
|
-
vendor: VENDOR_STANDARD_SCHEMA,
|
|
162
|
-
version: 1,
|
|
163
|
-
validate: (input) => {
|
|
164
|
-
const result = a['~standard'].validate(input);
|
|
165
|
-
const result2 = b['~standard'].validate(input);
|
|
166
|
-
|
|
167
|
-
if (result instanceof Promise || result2 instanceof Promise) {
|
|
168
|
-
throw new Error('Schema validation must be synchronous');
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
// TODO: Agree on the behavior of this for standard schemas
|
|
172
|
-
if (result.issues || result2.issues) {
|
|
173
|
-
return {
|
|
174
|
-
issues: [...(result.issues || []), ...(result2.issues || [])],
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
return {
|
|
179
|
-
value: {
|
|
180
|
-
...(result.value || {}),
|
|
181
|
-
...(result2.value || {}),
|
|
182
|
-
},
|
|
183
|
-
};
|
|
184
|
-
},
|
|
185
|
-
};
|
|
186
|
-
|
|
187
|
-
return {
|
|
188
|
-
'~standard': standardSchema,
|
|
189
|
-
};
|
|
190
|
-
};
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* Merges two header schemas together, these can either be legacy zod objects or objects containing standard schemas.
|
|
194
|
-
*/
|
|
195
|
-
export const mergeHeaderSchemasForRoute = (
|
|
196
|
-
baseSchema: unknown,
|
|
197
|
-
routeSchema: unknown,
|
|
198
|
-
): unknown => {
|
|
199
|
-
if (!baseSchema) {
|
|
200
|
-
return routeSchema;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
if (!routeSchema) {
|
|
204
|
-
return baseSchema;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
const mergedObjects = {
|
|
208
|
-
...baseSchema,
|
|
209
|
-
...routeSchema,
|
|
210
|
-
};
|
|
211
|
-
|
|
212
|
-
return mergedObjects;
|
|
213
|
-
};
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* Similar to validateAgainstStandardSchema, but it takes an unknown schema, it will not validate if no schema provided and will check the schema is
|
|
217
|
-
* valid before validating the data.
|
|
218
|
-
*
|
|
219
|
-
* This is super handy for validating request bodies, headers, etc. as it passes through the data if no schema is provided.
|
|
220
|
-
*/
|
|
221
|
-
export const validateIfSchema = (
|
|
222
|
-
data: unknown,
|
|
223
|
-
schema: unknown,
|
|
224
|
-
{
|
|
225
|
-
passThroughExtraKeys = false,
|
|
226
|
-
}: {
|
|
227
|
-
passThroughExtraKeys?: boolean;
|
|
228
|
-
} = {},
|
|
229
|
-
): {
|
|
230
|
-
value?: unknown;
|
|
231
|
-
error?: StandardSchemaError;
|
|
232
|
-
schemasUsed: Array<StandardSchemaV1<unknown, unknown>>;
|
|
233
|
-
} => {
|
|
234
|
-
const schemaStandard = parseAsStandardSchema(schema);
|
|
235
|
-
|
|
236
|
-
if (!schemaStandard) {
|
|
237
|
-
return { value: data, schemasUsed: [] };
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
const result = validateAgainstStandardSchema(data, schemaStandard, {
|
|
241
|
-
passThroughExtraKeys,
|
|
242
|
-
});
|
|
243
|
-
|
|
244
|
-
return {
|
|
245
|
-
...result,
|
|
246
|
-
schemasUsed: [schemaStandard],
|
|
247
|
-
};
|
|
248
|
-
};
|
|
249
|
-
|
|
250
|
-
export const validateAgainstStandardSchema = (
|
|
251
|
-
data: unknown,
|
|
252
|
-
schema: StandardSchemaV1<unknown, unknown>,
|
|
253
|
-
{
|
|
254
|
-
passThroughExtraKeys = false,
|
|
255
|
-
}: {
|
|
256
|
-
passThroughExtraKeys?: boolean;
|
|
257
|
-
} = {},
|
|
258
|
-
): { value?: unknown; error?: StandardSchemaError | ZodError } => {
|
|
259
|
-
const result = schema['~standard'].validate(data);
|
|
260
|
-
|
|
261
|
-
if (result instanceof Promise) {
|
|
262
|
-
throw new Error('Schema validation must be synchronous');
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
if (result.issues) {
|
|
266
|
-
return {
|
|
267
|
-
error: new StandardSchemaError(result.issues),
|
|
268
|
-
};
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
if (passThroughExtraKeys && typeof data === 'object' && result.value) {
|
|
272
|
-
return {
|
|
273
|
-
value: { ...data, ...result.value },
|
|
274
|
-
};
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
return {
|
|
278
|
-
value: result.value,
|
|
279
|
-
};
|
|
280
|
-
};
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
/** The Standard Schema interface. */
|
|
2
|
-
export interface StandardSchemaV1<Input = unknown, Output = Input> {
|
|
3
|
-
/** The Standard Schema properties. */
|
|
4
|
-
readonly '~standard': StandardSchemaV1.Props<Input, Output>;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
// eslint-disable-next-line @typescript-eslint/no-namespace
|
|
8
|
-
export declare namespace StandardSchemaV1 {
|
|
9
|
-
/** The Standard Schema properties interface. */
|
|
10
|
-
export interface Props<Input = unknown, Output = Input> {
|
|
11
|
-
/** The version number of the standard. */
|
|
12
|
-
readonly version: 1;
|
|
13
|
-
/** The vendor name of the schema library. */
|
|
14
|
-
readonly vendor: string;
|
|
15
|
-
/** Validates unknown input values. */
|
|
16
|
-
readonly validate: (
|
|
17
|
-
value: unknown,
|
|
18
|
-
) => Result<Output> | Promise<Result<Output>>;
|
|
19
|
-
/** Inferred types associated with the schema. */
|
|
20
|
-
readonly types?: Types<Input, Output> | undefined;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/** The result interface of the validate function. */
|
|
24
|
-
export type Result<Output> = SuccessResult<Output> | FailureResult;
|
|
25
|
-
|
|
26
|
-
/** The result interface if validation succeeds. */
|
|
27
|
-
export interface SuccessResult<Output> {
|
|
28
|
-
/** The typed output value. */
|
|
29
|
-
readonly value: Output;
|
|
30
|
-
/** The non-existent issues. */
|
|
31
|
-
readonly issues?: undefined;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/** The result interface if validation fails. */
|
|
35
|
-
export interface FailureResult {
|
|
36
|
-
/** The issues of failed validation. */
|
|
37
|
-
readonly issues: ReadonlyArray<Issue>;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/** The issue interface of the failure output. */
|
|
41
|
-
export interface Issue {
|
|
42
|
-
/** The error message of the issue. */
|
|
43
|
-
readonly message: string;
|
|
44
|
-
/** The path of the issue, if any. */
|
|
45
|
-
readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/** The path segment interface of the issue. */
|
|
49
|
-
export interface PathSegment {
|
|
50
|
-
/** The key representing a path segment. */
|
|
51
|
-
readonly key: PropertyKey;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/** The Standard Schema types interface. */
|
|
55
|
-
export interface Types<Input = unknown, Output = Input> {
|
|
56
|
-
/** The input type of the schema. */
|
|
57
|
-
readonly input: Input;
|
|
58
|
-
/** The output type of the schema. */
|
|
59
|
-
readonly output: Output;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/** Infers the input type of a Standard Schema. */
|
|
63
|
-
export type InferInput<Schema extends StandardSchemaV1> = NonNullable<
|
|
64
|
-
Schema['~standard']['types']
|
|
65
|
-
>['input'];
|
|
66
|
-
|
|
67
|
-
/** Infers the output type of a Standard Schema. */
|
|
68
|
-
export type InferOutput<Schema extends StandardSchemaV1> = NonNullable<
|
|
69
|
-
Schema['~standard']['types']
|
|
70
|
-
>['output'];
|
|
71
|
-
}
|
package/src/lib/status-codes.ts
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
export type SuccessfulHttpStatusCode =
|
|
2
|
-
| 200
|
|
3
|
-
| 201
|
|
4
|
-
| 202
|
|
5
|
-
| 203
|
|
6
|
-
| 204
|
|
7
|
-
| 205
|
|
8
|
-
| 206
|
|
9
|
-
| 207;
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* All available HTTP Status codes
|
|
13
|
-
*/
|
|
14
|
-
export type HTTPStatusCode =
|
|
15
|
-
| 100
|
|
16
|
-
| 101
|
|
17
|
-
| 102
|
|
18
|
-
| 200
|
|
19
|
-
| 201
|
|
20
|
-
| 202
|
|
21
|
-
| 203
|
|
22
|
-
| 204
|
|
23
|
-
| 205
|
|
24
|
-
| 206
|
|
25
|
-
| 207
|
|
26
|
-
| 300
|
|
27
|
-
| 301
|
|
28
|
-
| 302
|
|
29
|
-
| 303
|
|
30
|
-
| 304
|
|
31
|
-
| 305
|
|
32
|
-
| 307
|
|
33
|
-
| 308
|
|
34
|
-
| 400
|
|
35
|
-
| 401
|
|
36
|
-
| 402
|
|
37
|
-
| 403
|
|
38
|
-
| 404
|
|
39
|
-
| 405
|
|
40
|
-
| 406
|
|
41
|
-
| 407
|
|
42
|
-
| 408
|
|
43
|
-
| 409
|
|
44
|
-
| 410
|
|
45
|
-
| 411
|
|
46
|
-
| 412
|
|
47
|
-
| 413
|
|
48
|
-
| 414
|
|
49
|
-
| 415
|
|
50
|
-
| 416
|
|
51
|
-
| 417
|
|
52
|
-
| 418
|
|
53
|
-
| 419
|
|
54
|
-
| 420
|
|
55
|
-
| 421
|
|
56
|
-
| 422
|
|
57
|
-
| 423
|
|
58
|
-
| 424
|
|
59
|
-
| 428
|
|
60
|
-
| 429
|
|
61
|
-
| 431
|
|
62
|
-
| 451
|
|
63
|
-
| 500
|
|
64
|
-
| 501
|
|
65
|
-
| 502
|
|
66
|
-
| 503
|
|
67
|
-
| 504
|
|
68
|
-
| 505
|
|
69
|
-
| 507
|
|
70
|
-
| 511;
|
|
71
|
-
|
|
72
|
-
export type ErrorHttpStatusCode = Exclude<
|
|
73
|
-
HTTPStatusCode,
|
|
74
|
-
SuccessfulHttpStatusCode
|
|
75
|
-
>;
|