@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
package/index.cjs.js
ADDED
|
@@ -0,0 +1,807 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The error class for standard schema validation errors.
|
|
7
|
+
*
|
|
8
|
+
* @see {@link StandardSchemaV1.FailureResult}
|
|
9
|
+
*/
|
|
10
|
+
class StandardSchemaError extends Error {
|
|
11
|
+
constructor(issues) {
|
|
12
|
+
/**
|
|
13
|
+
* Similar pattern to ZodError regarding bigints.
|
|
14
|
+
*/
|
|
15
|
+
const message = JSON.stringify(issues, (_, value) => (typeof value === 'bigint' ? value.toString() : value), 2);
|
|
16
|
+
super(message);
|
|
17
|
+
this.name = 'ValidationError';
|
|
18
|
+
this.issues = issues;
|
|
19
|
+
}
|
|
20
|
+
/*
|
|
21
|
+
ZodError overrides the toString method to return the serialised message.
|
|
22
|
+
The Next.js implementation relies on this behaviour.
|
|
23
|
+
*/
|
|
24
|
+
toString() {
|
|
25
|
+
return this.message;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const VENDOR_STANDARD_SCHEMA = 'ts-rest-combined';
|
|
30
|
+
/**
|
|
31
|
+
* Type guard to check if the schema is a standard schema.
|
|
32
|
+
*
|
|
33
|
+
* @param schema - unknown
|
|
34
|
+
* @returns boolean
|
|
35
|
+
*/
|
|
36
|
+
const isStandardSchema = (schema) => {
|
|
37
|
+
if (!schema) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
const standard = schema === null || schema === void 0 ? void 0 : schema['~standard'];
|
|
41
|
+
if (!standard) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
return standard.version === 1 && typeof standard.validate === 'function';
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Takes in an unknown object and returns either a standard schema or null
|
|
48
|
+
*
|
|
49
|
+
* @param schema - unknown
|
|
50
|
+
* @returns StandardSchemaV1<unknown, unknown> | null
|
|
51
|
+
*/
|
|
52
|
+
const parseAsStandardSchema = (schema) => {
|
|
53
|
+
const isStandard = isStandardSchema(schema);
|
|
54
|
+
if (isStandard) {
|
|
55
|
+
return schema;
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Since 3.53.0 we've moved to headers using an object with schemas inside it, rather than a top level schema.
|
|
61
|
+
*
|
|
62
|
+
* This makes it easier to merge schemas together.
|
|
63
|
+
*
|
|
64
|
+
* @param data - Data to validate e.g. headers
|
|
65
|
+
* @param schemaObject - Schema object to validate against e.g. { 'x-foo': v.string() }
|
|
66
|
+
* @returns
|
|
67
|
+
*/
|
|
68
|
+
const validateMultiSchemaObject = (data, schemaObject) => {
|
|
69
|
+
const schema = parseAsStandardSchema(schemaObject);
|
|
70
|
+
// If the top level is not null we know it's a valid schema we can validate against
|
|
71
|
+
if (schema !== null) {
|
|
72
|
+
const result = validateAgainstStandardSchema(data, schema, {
|
|
73
|
+
passThroughExtraKeys: true,
|
|
74
|
+
});
|
|
75
|
+
return {
|
|
76
|
+
value: result.value,
|
|
77
|
+
error: result.error,
|
|
78
|
+
schemasUsed: [schema],
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
const headersMap = new Map(Object.entries(data !== null && data !== void 0 ? data : {}));
|
|
82
|
+
const vendorSet = new Set();
|
|
83
|
+
const subSchemas = new Map();
|
|
84
|
+
for (const [key, schema] of Object.entries(schemaObject !== null && schemaObject !== void 0 ? schemaObject : {})) {
|
|
85
|
+
const parsedSchema = parseAsStandardSchema(schema);
|
|
86
|
+
if (schema === null) {
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (typeof schema === 'symbol') {
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
if (parsedSchema !== null) {
|
|
93
|
+
subSchemas.set(key, parsedSchema);
|
|
94
|
+
vendorSet.add(parsedSchema['~standard'].vendor);
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
throw new Error(`Invalid schema provided for header ${key}, please use a valid schema`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (subSchemas.size === 0) {
|
|
101
|
+
return {
|
|
102
|
+
value: data,
|
|
103
|
+
schemasUsed: [],
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
const issues = [];
|
|
107
|
+
for (const [key, schema] of subSchemas.entries()) {
|
|
108
|
+
const value = headersMap.get(key);
|
|
109
|
+
const result = validateAgainstStandardSchema(value, schema);
|
|
110
|
+
if (result.error) {
|
|
111
|
+
for (const issue of result.error.issues) {
|
|
112
|
+
issues.push({
|
|
113
|
+
...issue,
|
|
114
|
+
path: [key],
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
headersMap.set(key, result.value);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
if (issues.length > 0) {
|
|
123
|
+
return {
|
|
124
|
+
error: new StandardSchemaError(issues),
|
|
125
|
+
schemasUsed: [...subSchemas.values()],
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
return {
|
|
129
|
+
value: Object.fromEntries(headersMap.entries()),
|
|
130
|
+
schemasUsed: [...subSchemas.values()],
|
|
131
|
+
};
|
|
132
|
+
};
|
|
133
|
+
/**
|
|
134
|
+
* Combines two standard schemas into a single standard schema.
|
|
135
|
+
*
|
|
136
|
+
* The combined schema will run the validation of both schemas and return the result of the first schema that
|
|
137
|
+
* succeeds.
|
|
138
|
+
*
|
|
139
|
+
* If either schema fails, the combined schema will return the issues from both schemas.
|
|
140
|
+
*
|
|
141
|
+
* @param a - StandardSchemaV1<unknown, unknown>
|
|
142
|
+
* @param b - StandardSchemaV1<unknown, unknown>
|
|
143
|
+
* @returns StandardSchemaV1<unknown, unknown>
|
|
144
|
+
*/
|
|
145
|
+
const combineStandardSchemas = (a, b) => {
|
|
146
|
+
const standardSchema = {
|
|
147
|
+
vendor: VENDOR_STANDARD_SCHEMA,
|
|
148
|
+
version: 1,
|
|
149
|
+
validate: (input) => {
|
|
150
|
+
const result = a['~standard'].validate(input);
|
|
151
|
+
const result2 = b['~standard'].validate(input);
|
|
152
|
+
if (result instanceof Promise || result2 instanceof Promise) {
|
|
153
|
+
throw new Error('Schema validation must be synchronous');
|
|
154
|
+
}
|
|
155
|
+
// TODO: Agree on the behavior of this for standard schemas
|
|
156
|
+
if (result.issues || result2.issues) {
|
|
157
|
+
return {
|
|
158
|
+
issues: [...(result.issues || []), ...(result2.issues || [])],
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
return {
|
|
162
|
+
value: {
|
|
163
|
+
...(result.value || {}),
|
|
164
|
+
...(result2.value || {}),
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
return {
|
|
170
|
+
'~standard': standardSchema,
|
|
171
|
+
};
|
|
172
|
+
};
|
|
173
|
+
/**
|
|
174
|
+
* Merges two header schemas together, these can either be legacy zod objects or objects containing standard schemas.
|
|
175
|
+
*/
|
|
176
|
+
const mergeHeaderSchemasForRoute = (baseSchema, routeSchema) => {
|
|
177
|
+
if (!baseSchema) {
|
|
178
|
+
return routeSchema;
|
|
179
|
+
}
|
|
180
|
+
if (!routeSchema) {
|
|
181
|
+
return baseSchema;
|
|
182
|
+
}
|
|
183
|
+
const mergedObjects = {
|
|
184
|
+
...baseSchema,
|
|
185
|
+
...routeSchema,
|
|
186
|
+
};
|
|
187
|
+
return mergedObjects;
|
|
188
|
+
};
|
|
189
|
+
/**
|
|
190
|
+
* Similar to validateAgainstStandardSchema, but it takes an unknown schema, it will not validate if no schema provided and will check the schema is
|
|
191
|
+
* valid before validating the data.
|
|
192
|
+
*
|
|
193
|
+
* This is super handy for validating request bodies, headers, etc. as it passes through the data if no schema is provided.
|
|
194
|
+
*/
|
|
195
|
+
const validateIfSchema = (data, schema, { passThroughExtraKeys = false, } = {}) => {
|
|
196
|
+
const schemaStandard = parseAsStandardSchema(schema);
|
|
197
|
+
if (!schemaStandard) {
|
|
198
|
+
return { value: data, schemasUsed: [] };
|
|
199
|
+
}
|
|
200
|
+
const result = validateAgainstStandardSchema(data, schemaStandard, {
|
|
201
|
+
passThroughExtraKeys,
|
|
202
|
+
});
|
|
203
|
+
return {
|
|
204
|
+
...result,
|
|
205
|
+
schemasUsed: [schemaStandard],
|
|
206
|
+
};
|
|
207
|
+
};
|
|
208
|
+
const validateAgainstStandardSchema = (data, schema, { passThroughExtraKeys = false, } = {}) => {
|
|
209
|
+
const result = schema['~standard'].validate(data);
|
|
210
|
+
if (result instanceof Promise) {
|
|
211
|
+
throw new Error('Schema validation must be synchronous');
|
|
212
|
+
}
|
|
213
|
+
if (result.issues) {
|
|
214
|
+
return {
|
|
215
|
+
error: new StandardSchemaError(result.issues),
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
if (passThroughExtraKeys && typeof data === 'object' && result.value) {
|
|
219
|
+
return {
|
|
220
|
+
value: { ...data, ...result.value },
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
return {
|
|
224
|
+
value: result.value,
|
|
225
|
+
};
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
const ContractNoBody = Symbol('ContractNoBody');
|
|
229
|
+
/**
|
|
230
|
+
* Differentiate between a route and a router
|
|
231
|
+
*
|
|
232
|
+
* @param obj
|
|
233
|
+
* @returns
|
|
234
|
+
*/
|
|
235
|
+
const isAppRoute = (obj) => {
|
|
236
|
+
return 'method' in obj && 'path' in obj;
|
|
237
|
+
};
|
|
238
|
+
const isAppRouteQuery = (route) => {
|
|
239
|
+
return route.method === 'GET';
|
|
240
|
+
};
|
|
241
|
+
const isAppRouteMutation = (route) => {
|
|
242
|
+
return !isAppRouteQuery(route);
|
|
243
|
+
};
|
|
244
|
+
/**
|
|
245
|
+
*
|
|
246
|
+
* @deprecated Please use {@link initContract} instead.
|
|
247
|
+
*/
|
|
248
|
+
const initTsRest = () => initContract();
|
|
249
|
+
const recursivelyApplyOptions = (router, options) => {
|
|
250
|
+
return Object.fromEntries(Object.entries(router).map(([key, value]) => {
|
|
251
|
+
var _a, _b, _c;
|
|
252
|
+
if (isAppRoute(value)) {
|
|
253
|
+
return [
|
|
254
|
+
key,
|
|
255
|
+
{
|
|
256
|
+
...value,
|
|
257
|
+
path: (options === null || options === void 0 ? void 0 : options.pathPrefix)
|
|
258
|
+
? options.pathPrefix + value.path
|
|
259
|
+
: value.path,
|
|
260
|
+
headers: mergeHeaderSchemasForRoute(options === null || options === void 0 ? void 0 : options.baseHeaders, value.headers),
|
|
261
|
+
strictStatusCodes: (_a = value.strictStatusCodes) !== null && _a !== void 0 ? _a : options === null || options === void 0 ? void 0 : options.strictStatusCodes,
|
|
262
|
+
validateResponseOnClient: (_b = value.validateResponseOnClient) !== null && _b !== void 0 ? _b : options === null || options === void 0 ? void 0 : options.validateResponseOnClient,
|
|
263
|
+
responses: {
|
|
264
|
+
...options === null || options === void 0 ? void 0 : options.commonResponses,
|
|
265
|
+
...value.responses,
|
|
266
|
+
},
|
|
267
|
+
metadata: (options === null || options === void 0 ? void 0 : options.metadata)
|
|
268
|
+
? {
|
|
269
|
+
...options === null || options === void 0 ? void 0 : options.metadata,
|
|
270
|
+
...((_c = value.metadata) !== null && _c !== void 0 ? _c : {}),
|
|
271
|
+
}
|
|
272
|
+
: value.metadata,
|
|
273
|
+
},
|
|
274
|
+
];
|
|
275
|
+
}
|
|
276
|
+
else {
|
|
277
|
+
return [key, recursivelyApplyOptions(value, options)];
|
|
278
|
+
}
|
|
279
|
+
}));
|
|
280
|
+
};
|
|
281
|
+
const ContractPlainTypeRuntimeSymbol = Symbol('ContractPlainType');
|
|
282
|
+
/**
|
|
283
|
+
* Instantiate a ts-rest client, primarily to access `router`, `response`, and `body`
|
|
284
|
+
*
|
|
285
|
+
* @returns {ContractInstance}
|
|
286
|
+
*/
|
|
287
|
+
const initContract = () => {
|
|
288
|
+
return {
|
|
289
|
+
// @ts-expect-error - this is a type error, but it's not clear how to fix it
|
|
290
|
+
router: (endpoints, options) => recursivelyApplyOptions(endpoints, options),
|
|
291
|
+
query: (args) => args,
|
|
292
|
+
mutation: (args) => args,
|
|
293
|
+
responses: (args) => args,
|
|
294
|
+
response: () => ContractPlainTypeRuntimeSymbol,
|
|
295
|
+
body: () => ContractPlainTypeRuntimeSymbol,
|
|
296
|
+
type: () => ContractPlainTypeRuntimeSymbol,
|
|
297
|
+
otherResponse: ({ contentType, body, }) => ({
|
|
298
|
+
contentType,
|
|
299
|
+
body,
|
|
300
|
+
}),
|
|
301
|
+
noBody: () => ContractNoBody,
|
|
302
|
+
};
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* @param path - The URL e.g. /posts/:id
|
|
307
|
+
* @param params - The params e.g. `{ id: string }`
|
|
308
|
+
* @returns - The URL with the params e.g. /posts/123
|
|
309
|
+
*/
|
|
310
|
+
const insertParamsIntoPath = ({ path, params, }) => {
|
|
311
|
+
const pathParams = params;
|
|
312
|
+
return path.replace(/\/?:([^/?]+)\??/g, (matched, p) => pathParams[p]
|
|
313
|
+
? `${matched.startsWith('/') ? '/' : ''}${pathParams[p]}`
|
|
314
|
+
: '');
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
*
|
|
319
|
+
* @param query - Any JSON object
|
|
320
|
+
* @param json - Use JSON.stringify to encode the query values
|
|
321
|
+
* @returns - The query url segment, using explode array syntax, and deep object syntax
|
|
322
|
+
*/
|
|
323
|
+
const convertQueryParamsToUrlString = (query, json = false) => {
|
|
324
|
+
const queryString = json
|
|
325
|
+
? encodeQueryParamsJson(query)
|
|
326
|
+
: encodeQueryParams(query);
|
|
327
|
+
return (queryString === null || queryString === void 0 ? void 0 : queryString.length) > 0 ? '?' + queryString : '';
|
|
328
|
+
};
|
|
329
|
+
const encodeQueryParamsJson = (query) => {
|
|
330
|
+
if (!query) {
|
|
331
|
+
return '';
|
|
332
|
+
}
|
|
333
|
+
return Object.entries(query)
|
|
334
|
+
.filter(([, value]) => value !== undefined)
|
|
335
|
+
.map(([key, value]) => {
|
|
336
|
+
let encodedValue;
|
|
337
|
+
// if value is a string and is not a reserved JSON value or a number, pass it without encoding
|
|
338
|
+
// this makes strings look nicer in the URL (e.g. ?name=John instead of ?name=%22John%22)
|
|
339
|
+
// this is also how OpenAPI will pass strings even if they are marked as application/json types
|
|
340
|
+
if (typeof value === 'string' &&
|
|
341
|
+
!['true', 'false', 'null'].includes(value.trim()) &&
|
|
342
|
+
isNaN(Number(value))) {
|
|
343
|
+
encodedValue = value;
|
|
344
|
+
}
|
|
345
|
+
else {
|
|
346
|
+
encodedValue = JSON.stringify(value);
|
|
347
|
+
}
|
|
348
|
+
return `${encodeURIComponent(key)}=${encodeURIComponent(encodedValue)}`;
|
|
349
|
+
})
|
|
350
|
+
.join('&');
|
|
351
|
+
};
|
|
352
|
+
const encodeQueryParams = (query) => {
|
|
353
|
+
if (!query) {
|
|
354
|
+
return '';
|
|
355
|
+
}
|
|
356
|
+
return (Object.keys(query)
|
|
357
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
358
|
+
// @ts-ignore
|
|
359
|
+
.flatMap((key) => tokeniseValue(key, query[key]))
|
|
360
|
+
.map(([key, value]) => {
|
|
361
|
+
return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
|
|
362
|
+
})
|
|
363
|
+
.join('&'));
|
|
364
|
+
};
|
|
365
|
+
/**
|
|
366
|
+
* A recursive function to convert an object/string/number/Date/whatever into an array of key=value pairs
|
|
367
|
+
*
|
|
368
|
+
* The output of this should be flatMap-able to a string of key=value pairs which can be
|
|
369
|
+
* joined with & to form a query string
|
|
370
|
+
*
|
|
371
|
+
* This should be fully compatible with the "qs" library, but without the need to add a dependency
|
|
372
|
+
*/
|
|
373
|
+
const tokeniseValue = (key, value) => {
|
|
374
|
+
if (Array.isArray(value)) {
|
|
375
|
+
return value.flatMap((v, idx) => tokeniseValue(`${key}[${idx}]`, v));
|
|
376
|
+
}
|
|
377
|
+
if (value instanceof Date) {
|
|
378
|
+
return [[`${key}`, value.toISOString()]];
|
|
379
|
+
}
|
|
380
|
+
if (value === null) {
|
|
381
|
+
return [[`${key}`, '']];
|
|
382
|
+
}
|
|
383
|
+
if (value === undefined) {
|
|
384
|
+
return [];
|
|
385
|
+
}
|
|
386
|
+
if (typeof value === 'object') {
|
|
387
|
+
return Object.keys(value).flatMap((k) =>
|
|
388
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
389
|
+
// @ts-ignore
|
|
390
|
+
tokeniseValue(`${key}[${k}]`, value[k]));
|
|
391
|
+
}
|
|
392
|
+
return [[`${key}`, `${value}`]];
|
|
393
|
+
};
|
|
394
|
+
/**
|
|
395
|
+
*
|
|
396
|
+
* @param query - A server-side query object where values have been encoded as JSON strings
|
|
397
|
+
* @returns - The same object with the JSON strings decoded. Objects that were encoded using toJSON such as Dates will remain as strings
|
|
398
|
+
*/
|
|
399
|
+
const parseJsonQueryObject = (query) => {
|
|
400
|
+
return Object.fromEntries(Object.entries(query).map(([key, value]) => {
|
|
401
|
+
let parsedValue;
|
|
402
|
+
// if json parse fails, treat the value as a string
|
|
403
|
+
// this allows us to pass strings without having to surround them with quotes
|
|
404
|
+
try {
|
|
405
|
+
parsedValue = JSON.parse(value);
|
|
406
|
+
}
|
|
407
|
+
catch {
|
|
408
|
+
parsedValue = value;
|
|
409
|
+
}
|
|
410
|
+
return [key, parsedValue];
|
|
411
|
+
}));
|
|
412
|
+
};
|
|
413
|
+
|
|
414
|
+
class UnknownStatusError extends Error {
|
|
415
|
+
constructor(response, knownResponseStatuses) {
|
|
416
|
+
const expectedStatuses = knownResponseStatuses.join(',');
|
|
417
|
+
super(`Server returned unexpected response. Expected one of: ${expectedStatuses} got: ${response.status}`);
|
|
418
|
+
this.response = response;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* @deprecated Only safe to use on the client-side. Use `ServerInferResponses`/`ClientInferResponses` instead.
|
|
424
|
+
*/
|
|
425
|
+
function getRouteResponses(router) {
|
|
426
|
+
return {};
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* Default fetch api implementation:
|
|
430
|
+
*
|
|
431
|
+
* Can be used as a reference for implementing your own fetcher,
|
|
432
|
+
* or used in the "api" field of ClientArgs to allow you to hook
|
|
433
|
+
* into the request to run custom logic
|
|
434
|
+
*/
|
|
435
|
+
const tsRestFetchApi = async ({ route, path, method, headers, body, validateResponse, fetchOptions, }) => {
|
|
436
|
+
const result = await fetch(path, {
|
|
437
|
+
...fetchOptions,
|
|
438
|
+
method,
|
|
439
|
+
headers,
|
|
440
|
+
body,
|
|
441
|
+
});
|
|
442
|
+
const contentType = result.headers.get('content-type');
|
|
443
|
+
if ((contentType === null || contentType === void 0 ? void 0 : contentType.includes('application/')) && (contentType === null || contentType === void 0 ? void 0 : contentType.includes('json'))) {
|
|
444
|
+
const responseSchema = route.responses[result.status];
|
|
445
|
+
const response = {
|
|
446
|
+
status: result.status,
|
|
447
|
+
body: responseSchema === ContractNoBody ? undefined : await result.json(),
|
|
448
|
+
headers: result.headers,
|
|
449
|
+
};
|
|
450
|
+
const responseSchemaStandard = parseAsStandardSchema(responseSchema);
|
|
451
|
+
if (responseSchemaStandard &&
|
|
452
|
+
(validateResponse !== null && validateResponse !== void 0 ? validateResponse : route.validateResponseOnClient)) {
|
|
453
|
+
const result = validateAgainstStandardSchema(response.body, responseSchemaStandard);
|
|
454
|
+
if (result.error) {
|
|
455
|
+
throw result.error;
|
|
456
|
+
}
|
|
457
|
+
return {
|
|
458
|
+
...response,
|
|
459
|
+
body: result.value,
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
return response;
|
|
463
|
+
}
|
|
464
|
+
if (contentType === null || contentType === void 0 ? void 0 : contentType.includes('text/')) {
|
|
465
|
+
return {
|
|
466
|
+
status: result.status,
|
|
467
|
+
body: await result.text(),
|
|
468
|
+
headers: result.headers,
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
return {
|
|
472
|
+
status: result.status,
|
|
473
|
+
body: await result.blob(),
|
|
474
|
+
headers: result.headers,
|
|
475
|
+
};
|
|
476
|
+
};
|
|
477
|
+
const createFormData = (body) => {
|
|
478
|
+
const formData = new FormData();
|
|
479
|
+
const appendToFormData = (key, value) => {
|
|
480
|
+
if (value instanceof File) {
|
|
481
|
+
formData.append(key, value);
|
|
482
|
+
}
|
|
483
|
+
else {
|
|
484
|
+
formData.append(key, JSON.stringify(value));
|
|
485
|
+
}
|
|
486
|
+
};
|
|
487
|
+
Object.entries(body).forEach(([key, value]) => {
|
|
488
|
+
if (Array.isArray(value)) {
|
|
489
|
+
for (const item of value) {
|
|
490
|
+
appendToFormData(key, item);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
else {
|
|
494
|
+
appendToFormData(key, value);
|
|
495
|
+
}
|
|
496
|
+
});
|
|
497
|
+
return formData;
|
|
498
|
+
};
|
|
499
|
+
const normalizeHeaders = (headers) => {
|
|
500
|
+
return Object.fromEntries(Object.entries(headers).map(([k, v]) => [k.toLowerCase(), v]));
|
|
501
|
+
};
|
|
502
|
+
const fetchApi = (options) => {
|
|
503
|
+
const { path, clientArgs, route, body, query, extraInputArgs, headers, fetchOptions, } = options;
|
|
504
|
+
const apiFetcher = clientArgs.api || tsRestFetchApi;
|
|
505
|
+
const baseHeaders = clientArgs.baseHeaders &&
|
|
506
|
+
Object.fromEntries(Object.entries(clientArgs.baseHeaders).map(([name, valueOrFunction]) => {
|
|
507
|
+
if (typeof valueOrFunction === 'function') {
|
|
508
|
+
return [name, valueOrFunction(options)];
|
|
509
|
+
}
|
|
510
|
+
else {
|
|
511
|
+
return [name, valueOrFunction];
|
|
512
|
+
}
|
|
513
|
+
}));
|
|
514
|
+
const combinedHeaders = {
|
|
515
|
+
...(baseHeaders && normalizeHeaders(baseHeaders)),
|
|
516
|
+
...normalizeHeaders(headers),
|
|
517
|
+
};
|
|
518
|
+
// Remove any headers that are set to undefined
|
|
519
|
+
Object.keys(combinedHeaders).forEach((key) => {
|
|
520
|
+
if (combinedHeaders[key] === undefined) {
|
|
521
|
+
delete combinedHeaders[key];
|
|
522
|
+
}
|
|
523
|
+
});
|
|
524
|
+
let fetcherArgs = {
|
|
525
|
+
route,
|
|
526
|
+
path,
|
|
527
|
+
method: route.method,
|
|
528
|
+
headers: combinedHeaders,
|
|
529
|
+
body: undefined,
|
|
530
|
+
rawBody: body,
|
|
531
|
+
rawQuery: query,
|
|
532
|
+
contentType: undefined,
|
|
533
|
+
validateResponse: clientArgs.validateResponse,
|
|
534
|
+
fetchOptions: {
|
|
535
|
+
...(clientArgs.credentials && { credentials: clientArgs.credentials }),
|
|
536
|
+
...fetchOptions,
|
|
537
|
+
},
|
|
538
|
+
...((fetchOptions === null || fetchOptions === void 0 ? void 0 : fetchOptions.signal) && { signal: fetchOptions.signal }),
|
|
539
|
+
...((fetchOptions === null || fetchOptions === void 0 ? void 0 : fetchOptions.cache) && { cache: fetchOptions.cache }),
|
|
540
|
+
...(fetchOptions &&
|
|
541
|
+
'next' in fetchOptions &&
|
|
542
|
+
!!(fetchOptions === null || fetchOptions === void 0 ? void 0 : fetchOptions.next) && { next: fetchOptions.next }),
|
|
543
|
+
};
|
|
544
|
+
if (route.method !== 'GET') {
|
|
545
|
+
if ('contentType' in route && route.contentType === 'multipart/form-data') {
|
|
546
|
+
fetcherArgs = {
|
|
547
|
+
...fetcherArgs,
|
|
548
|
+
contentType: 'multipart/form-data',
|
|
549
|
+
body: body instanceof FormData ? body : createFormData(body),
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
else if ('contentType' in route &&
|
|
553
|
+
route.contentType === 'application/x-www-form-urlencoded') {
|
|
554
|
+
fetcherArgs = {
|
|
555
|
+
...fetcherArgs,
|
|
556
|
+
contentType: 'application/x-www-form-urlencoded',
|
|
557
|
+
headers: {
|
|
558
|
+
'content-type': 'application/x-www-form-urlencoded',
|
|
559
|
+
...fetcherArgs.headers,
|
|
560
|
+
},
|
|
561
|
+
body: typeof body === 'string'
|
|
562
|
+
? body
|
|
563
|
+
: new URLSearchParams(body),
|
|
564
|
+
};
|
|
565
|
+
}
|
|
566
|
+
else if (body !== null && body !== undefined) {
|
|
567
|
+
fetcherArgs = {
|
|
568
|
+
...fetcherArgs,
|
|
569
|
+
contentType: 'application/json',
|
|
570
|
+
headers: {
|
|
571
|
+
'content-type': 'application/json',
|
|
572
|
+
...fetcherArgs.headers,
|
|
573
|
+
},
|
|
574
|
+
body: JSON.stringify(body),
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
return apiFetcher({
|
|
579
|
+
...fetcherArgs,
|
|
580
|
+
...extraInputArgs,
|
|
581
|
+
});
|
|
582
|
+
};
|
|
583
|
+
const evaluateFetchApiArgs = (route, clientArgs, inputArgs) => {
|
|
584
|
+
const { query, params, body, headers, extraHeaders, overrideClientOptions, fetchOptions,
|
|
585
|
+
// TODO: remove in 4.0
|
|
586
|
+
cache,
|
|
587
|
+
// TODO: remove in 4.0
|
|
588
|
+
next,
|
|
589
|
+
// extra input args
|
|
590
|
+
...extraInputArgs } = inputArgs || {};
|
|
591
|
+
const overriddenClientArgs = {
|
|
592
|
+
...clientArgs,
|
|
593
|
+
...overrideClientOptions,
|
|
594
|
+
};
|
|
595
|
+
/**
|
|
596
|
+
* Coerce params to strings, allowing for numbers to be passed in (e.g. from using z.coerce.number())
|
|
597
|
+
*/
|
|
598
|
+
const parsedParams = typeof params === 'object'
|
|
599
|
+
? Object.fromEntries(Object.entries(params).map(([key, value]) => [key, String(value)]))
|
|
600
|
+
: {};
|
|
601
|
+
const completeUrl = getCompleteUrl(query, overriddenClientArgs.baseUrl, parsedParams, route, !!overriddenClientArgs.jsonQuery);
|
|
602
|
+
return {
|
|
603
|
+
path: completeUrl,
|
|
604
|
+
clientArgs: overriddenClientArgs,
|
|
605
|
+
route,
|
|
606
|
+
body,
|
|
607
|
+
query,
|
|
608
|
+
extraInputArgs,
|
|
609
|
+
fetchOptions: {
|
|
610
|
+
...(cache && { cache }),
|
|
611
|
+
...(next && { next }),
|
|
612
|
+
...fetchOptions,
|
|
613
|
+
},
|
|
614
|
+
headers: {
|
|
615
|
+
...extraHeaders,
|
|
616
|
+
...headers,
|
|
617
|
+
},
|
|
618
|
+
};
|
|
619
|
+
};
|
|
620
|
+
/**
|
|
621
|
+
* @hidden
|
|
622
|
+
*/
|
|
623
|
+
const getCompleteUrl = (query, baseUrl, params, route, jsonQuery) => {
|
|
624
|
+
const path = insertParamsIntoPath({
|
|
625
|
+
path: route.path,
|
|
626
|
+
params: params,
|
|
627
|
+
});
|
|
628
|
+
const queryComponent = convertQueryParamsToUrlString(query, jsonQuery);
|
|
629
|
+
if (baseUrl.endsWith('/') && path.startsWith('/')) {
|
|
630
|
+
return `${baseUrl}${path.substring(1)}${queryComponent}`;
|
|
631
|
+
}
|
|
632
|
+
return `${baseUrl}${path}${queryComponent}`;
|
|
633
|
+
};
|
|
634
|
+
const getRouteQuery = (route, clientArgs) => {
|
|
635
|
+
const knownResponseStatuses = Object.keys(route.responses);
|
|
636
|
+
return async (inputArgs) => {
|
|
637
|
+
const fetchApiArgs = evaluateFetchApiArgs(route, clientArgs, inputArgs);
|
|
638
|
+
const response = await fetchApi(fetchApiArgs);
|
|
639
|
+
// TODO: in next major version, throw by default if `strictStatusCode` is enabled
|
|
640
|
+
if (!clientArgs.throwOnUnknownStatus) {
|
|
641
|
+
return response;
|
|
642
|
+
}
|
|
643
|
+
if (knownResponseStatuses.includes(response.status.toString())) {
|
|
644
|
+
return response;
|
|
645
|
+
}
|
|
646
|
+
throw new UnknownStatusError(response, knownResponseStatuses);
|
|
647
|
+
};
|
|
648
|
+
};
|
|
649
|
+
const initClient = (router, args) => {
|
|
650
|
+
return Object.fromEntries(Object.entries(router).map(([key, subRouter]) => {
|
|
651
|
+
if (isAppRoute(subRouter)) {
|
|
652
|
+
return [key, getRouteQuery(subRouter, args)];
|
|
653
|
+
}
|
|
654
|
+
else {
|
|
655
|
+
return [key, initClient(subRouter, args)];
|
|
656
|
+
}
|
|
657
|
+
}));
|
|
658
|
+
};
|
|
659
|
+
|
|
660
|
+
class TsRestResponseValidationError extends Error {
|
|
661
|
+
constructor(appRoute, cause) {
|
|
662
|
+
super(`[ts-rest] Response validation failed for ${appRoute.method} ${appRoute.path}: ${cause.message}`);
|
|
663
|
+
this.appRoute = appRoute;
|
|
664
|
+
this.cause = cause;
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
class TsRestRequestValidationError extends Error {
|
|
668
|
+
constructor(pathParams, headers, query, body) {
|
|
669
|
+
super('[ts-rest] request validation failed');
|
|
670
|
+
this.pathParams = pathParams;
|
|
671
|
+
this.headers = headers;
|
|
672
|
+
this.query = query;
|
|
673
|
+
this.body = body;
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
const isAppRouteResponse = (value) => {
|
|
678
|
+
return (value != null &&
|
|
679
|
+
typeof value === 'object' &&
|
|
680
|
+
'status' in value &&
|
|
681
|
+
typeof value.status === 'number');
|
|
682
|
+
};
|
|
683
|
+
const isAppRouteOtherResponse = (response) => {
|
|
684
|
+
return (response != null &&
|
|
685
|
+
typeof response === 'object' &&
|
|
686
|
+
'contentType' in response);
|
|
687
|
+
};
|
|
688
|
+
const isAppRouteNoBody = (response) => {
|
|
689
|
+
return response === ContractNoBody;
|
|
690
|
+
};
|
|
691
|
+
const validateResponse = ({ appRoute, response, }) => {
|
|
692
|
+
if (isAppRouteResponse(response)) {
|
|
693
|
+
const responseType = appRoute.responses[response.status];
|
|
694
|
+
const responseSchema = isAppRouteOtherResponse(responseType)
|
|
695
|
+
? responseType.body
|
|
696
|
+
: responseType;
|
|
697
|
+
const responseStandardSchema = parseAsStandardSchema(responseSchema);
|
|
698
|
+
const responseValidation = validateIfSchema(response.body, responseStandardSchema);
|
|
699
|
+
if (responseValidation.error) {
|
|
700
|
+
throw new TsRestResponseValidationError(appRoute, responseValidation.error);
|
|
701
|
+
}
|
|
702
|
+
return {
|
|
703
|
+
status: response.status,
|
|
704
|
+
body: responseValidation.value,
|
|
705
|
+
};
|
|
706
|
+
}
|
|
707
|
+
return response;
|
|
708
|
+
};
|
|
709
|
+
|
|
710
|
+
class TsRestResponseError extends Error {
|
|
711
|
+
constructor(route, response) {
|
|
712
|
+
super();
|
|
713
|
+
this.statusCode = response.status;
|
|
714
|
+
this.body = response.body;
|
|
715
|
+
this.name = this.constructor.name;
|
|
716
|
+
if (typeof response.body === 'string') {
|
|
717
|
+
this.message = response.body;
|
|
718
|
+
}
|
|
719
|
+
else if (typeof response.body === 'object' &&
|
|
720
|
+
response.body !== null &&
|
|
721
|
+
'message' in response.body &&
|
|
722
|
+
typeof response.body.message === 'string') {
|
|
723
|
+
this.message = response.body['message'];
|
|
724
|
+
}
|
|
725
|
+
else {
|
|
726
|
+
this.message = 'Error';
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
const isResponse = (response, contractEndpoint) => {
|
|
732
|
+
return (typeof response === 'object' &&
|
|
733
|
+
response !== null &&
|
|
734
|
+
'status' in response &&
|
|
735
|
+
'body' in response &&
|
|
736
|
+
typeof response.status === 'number' &&
|
|
737
|
+
response.status >= 200 &&
|
|
738
|
+
response.status < 600 &&
|
|
739
|
+
((contractEndpoint === null || contractEndpoint === void 0 ? void 0 : contractEndpoint.strictStatusCodes)
|
|
740
|
+
? Object.keys(contractEndpoint.responses).includes(response.status.toString())
|
|
741
|
+
: true));
|
|
742
|
+
};
|
|
743
|
+
const isSuccessResponse = (response, contractEndpoint) => {
|
|
744
|
+
return (isResponse(response, contractEndpoint) &&
|
|
745
|
+
response.status >= 200 &&
|
|
746
|
+
response.status < 300);
|
|
747
|
+
};
|
|
748
|
+
const isErrorResponse = (response, contractEndpoint) => {
|
|
749
|
+
return (isResponse(response, contractEndpoint) &&
|
|
750
|
+
!isSuccessResponse(response, contractEndpoint));
|
|
751
|
+
};
|
|
752
|
+
const isUnknownResponse = (response, contractEndpoint) => {
|
|
753
|
+
return (isResponse(response) &&
|
|
754
|
+
!Object.keys(contractEndpoint.responses).includes(response.status.toString()));
|
|
755
|
+
};
|
|
756
|
+
const isUnknownSuccessResponse = (response, contractEndpoint) => {
|
|
757
|
+
return (isSuccessResponse(response) && isUnknownResponse(response, contractEndpoint));
|
|
758
|
+
};
|
|
759
|
+
const isUnknownErrorResponse = (response, contractEndpoint) => {
|
|
760
|
+
return (isErrorResponse(response) && isUnknownResponse(response, contractEndpoint));
|
|
761
|
+
};
|
|
762
|
+
const exhaustiveGuard = (response) => {
|
|
763
|
+
throw new Error(`Unreachable code: Response status is ${response.status}`);
|
|
764
|
+
};
|
|
765
|
+
|
|
766
|
+
exports.ContractNoBody = ContractNoBody;
|
|
767
|
+
exports.ContractPlainTypeRuntimeSymbol = ContractPlainTypeRuntimeSymbol;
|
|
768
|
+
exports.StandardSchemaError = StandardSchemaError;
|
|
769
|
+
exports.TsRestRequestValidationError = TsRestRequestValidationError;
|
|
770
|
+
exports.TsRestResponseError = TsRestResponseError;
|
|
771
|
+
exports.TsRestResponseValidationError = TsRestResponseValidationError;
|
|
772
|
+
exports.UnknownStatusError = UnknownStatusError;
|
|
773
|
+
exports.combineStandardSchemas = combineStandardSchemas;
|
|
774
|
+
exports.convertQueryParamsToUrlString = convertQueryParamsToUrlString;
|
|
775
|
+
exports.encodeQueryParams = encodeQueryParams;
|
|
776
|
+
exports.encodeQueryParamsJson = encodeQueryParamsJson;
|
|
777
|
+
exports.evaluateFetchApiArgs = evaluateFetchApiArgs;
|
|
778
|
+
exports.exhaustiveGuard = exhaustiveGuard;
|
|
779
|
+
exports.fetchApi = fetchApi;
|
|
780
|
+
exports.getCompleteUrl = getCompleteUrl;
|
|
781
|
+
exports.getRouteQuery = getRouteQuery;
|
|
782
|
+
exports.getRouteResponses = getRouteResponses;
|
|
783
|
+
exports.initClient = initClient;
|
|
784
|
+
exports.initContract = initContract;
|
|
785
|
+
exports.initTsRest = initTsRest;
|
|
786
|
+
exports.insertParamsIntoPath = insertParamsIntoPath;
|
|
787
|
+
exports.isAppRoute = isAppRoute;
|
|
788
|
+
exports.isAppRouteMutation = isAppRouteMutation;
|
|
789
|
+
exports.isAppRouteNoBody = isAppRouteNoBody;
|
|
790
|
+
exports.isAppRouteOtherResponse = isAppRouteOtherResponse;
|
|
791
|
+
exports.isAppRouteQuery = isAppRouteQuery;
|
|
792
|
+
exports.isAppRouteResponse = isAppRouteResponse;
|
|
793
|
+
exports.isErrorResponse = isErrorResponse;
|
|
794
|
+
exports.isResponse = isResponse;
|
|
795
|
+
exports.isStandardSchema = isStandardSchema;
|
|
796
|
+
exports.isSuccessResponse = isSuccessResponse;
|
|
797
|
+
exports.isUnknownErrorResponse = isUnknownErrorResponse;
|
|
798
|
+
exports.isUnknownResponse = isUnknownResponse;
|
|
799
|
+
exports.isUnknownSuccessResponse = isUnknownSuccessResponse;
|
|
800
|
+
exports.mergeHeaderSchemasForRoute = mergeHeaderSchemasForRoute;
|
|
801
|
+
exports.parseAsStandardSchema = parseAsStandardSchema;
|
|
802
|
+
exports.parseJsonQueryObject = parseJsonQueryObject;
|
|
803
|
+
exports.tsRestFetchApi = tsRestFetchApi;
|
|
804
|
+
exports.validateAgainstStandardSchema = validateAgainstStandardSchema;
|
|
805
|
+
exports.validateIfSchema = validateIfSchema;
|
|
806
|
+
exports.validateMultiSchemaObject = validateMultiSchemaObject;
|
|
807
|
+
exports.validateResponse = validateResponse;
|