@scalar/oas-utils 0.11.1 → 0.12.0

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.
Files changed (126) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/dist/helpers/index.d.ts +0 -82
  3. package/dist/helpers/index.d.ts.map +1 -1
  4. package/dist/helpers/index.js +0 -78
  5. package/dist/migrations/migrate-to-indexdb.d.ts.map +1 -1
  6. package/dist/migrations/migrate-to-indexdb.js +1 -1
  7. package/dist/migrations/v-2.5.0/types.generated.d.ts +428 -34
  8. package/dist/migrations/v-2.5.0/types.generated.d.ts.map +1 -1
  9. package/package.json +7 -61
  10. package/dist/entities/cookie/cookie.d.ts +0 -15
  11. package/dist/entities/cookie/cookie.d.ts.map +0 -1
  12. package/dist/entities/cookie/cookie.js +0 -12
  13. package/dist/entities/cookie/index.d.ts +0 -2
  14. package/dist/entities/cookie/index.d.ts.map +0 -1
  15. package/dist/entities/cookie/index.js +0 -1
  16. package/dist/entities/environment/environment.d.ts +0 -12
  17. package/dist/entities/environment/environment.d.ts.map +0 -1
  18. package/dist/entities/environment/environment.js +0 -8
  19. package/dist/entities/environment/index.d.ts +0 -2
  20. package/dist/entities/environment/index.d.ts.map +0 -1
  21. package/dist/entities/environment/index.js +0 -1
  22. package/dist/entities/hotkeys/hotkeys.d.ts +0 -15
  23. package/dist/entities/hotkeys/hotkeys.d.ts.map +0 -1
  24. package/dist/entities/hotkeys/hotkeys.js +0 -118
  25. package/dist/entities/hotkeys/index.d.ts +0 -2
  26. package/dist/entities/hotkeys/index.d.ts.map +0 -1
  27. package/dist/entities/hotkeys/index.js +0 -1
  28. package/dist/entities/shared/index.d.ts +0 -2
  29. package/dist/entities/shared/index.d.ts.map +0 -1
  30. package/dist/entities/shared/index.js +0 -1
  31. package/dist/entities/shared/utility.d.ts +0 -5
  32. package/dist/entities/shared/utility.d.ts.map +0 -1
  33. package/dist/entities/shared/utility.js +0 -9
  34. package/dist/entities/spec/collection.d.ts +0 -133
  35. package/dist/entities/spec/collection.d.ts.map +0 -1
  36. package/dist/entities/spec/collection.js +0 -95
  37. package/dist/entities/spec/index.d.ts +0 -12
  38. package/dist/entities/spec/index.d.ts.map +0 -1
  39. package/dist/entities/spec/index.js +0 -10
  40. package/dist/entities/spec/operation.d.ts +0 -99
  41. package/dist/entities/spec/operation.d.ts.map +0 -1
  42. package/dist/entities/spec/operation.js +0 -6
  43. package/dist/entities/spec/parameters.d.ts +0 -44
  44. package/dist/entities/spec/parameters.d.ts.map +0 -1
  45. package/dist/entities/spec/parameters.js +0 -41
  46. package/dist/entities/spec/request-examples.d.ts +0 -380
  47. package/dist/entities/spec/request-examples.d.ts.map +0 -1
  48. package/dist/entities/spec/request-examples.js +0 -449
  49. package/dist/entities/spec/requests.d.ts +0 -137
  50. package/dist/entities/spec/requests.d.ts.map +0 -1
  51. package/dist/entities/spec/requests.js +0 -123
  52. package/dist/entities/spec/server.d.ts +0 -15
  53. package/dist/entities/spec/server.d.ts.map +0 -1
  54. package/dist/entities/spec/server.js +0 -67
  55. package/dist/entities/spec/spec-objects.d.ts +0 -171
  56. package/dist/entities/spec/spec-objects.d.ts.map +0 -1
  57. package/dist/entities/spec/spec-objects.js +0 -134
  58. package/dist/entities/spec/x-scalar-environments.d.ts +0 -22
  59. package/dist/entities/spec/x-scalar-environments.d.ts.map +0 -1
  60. package/dist/entities/spec/x-scalar-environments.js +0 -22
  61. package/dist/entities/spec/x-scalar-secrets.d.ts +0 -6
  62. package/dist/entities/spec/x-scalar-secrets.d.ts.map +0 -1
  63. package/dist/entities/spec/x-scalar-secrets.js +0 -8
  64. package/dist/entities/workspace/index.d.ts +0 -2
  65. package/dist/entities/workspace/index.d.ts.map +0 -1
  66. package/dist/entities/workspace/index.js +0 -1
  67. package/dist/entities/workspace/workspace.d.ts +0 -236
  68. package/dist/entities/workspace/workspace.d.ts.map +0 -1
  69. package/dist/entities/workspace/workspace.js +0 -49
  70. package/dist/helpers/fetch-document.d.ts +0 -7
  71. package/dist/helpers/fetch-document.d.ts.map +0 -1
  72. package/dist/helpers/fetch-document.js +0 -32
  73. package/dist/helpers/fetch-with-proxy-fallback.d.ts +0 -15
  74. package/dist/helpers/fetch-with-proxy-fallback.d.ts.map +0 -1
  75. package/dist/helpers/fetch-with-proxy-fallback.js +0 -29
  76. package/dist/helpers/normalize-mime-type-object.d.ts +0 -8
  77. package/dist/helpers/normalize-mime-type-object.d.ts.map +0 -1
  78. package/dist/helpers/normalize-mime-type-object.js +0 -21
  79. package/dist/helpers/normalize-mime-type.d.ts +0 -10
  80. package/dist/helpers/normalize-mime-type.d.ts.map +0 -1
  81. package/dist/helpers/normalize-mime-type.js +0 -12
  82. package/dist/helpers/operation-stability.d.ts +0 -19
  83. package/dist/helpers/operation-stability.d.ts.map +0 -1
  84. package/dist/helpers/operation-stability.js +0 -24
  85. package/dist/helpers/schema-model.d.ts +0 -5
  86. package/dist/helpers/schema-model.d.ts.map +0 -1
  87. package/dist/helpers/schema-model.js +0 -14
  88. package/dist/helpers/security/get-schemes.d.ts +0 -97
  89. package/dist/helpers/security/get-schemes.d.ts.map +0 -1
  90. package/dist/helpers/security/get-schemes.js +0 -8
  91. package/dist/helpers/security/has-token.d.ts +0 -4
  92. package/dist/helpers/security/has-token.d.ts.map +0 -1
  93. package/dist/helpers/security/has-token.js +0 -19
  94. package/dist/helpers/security/index.d.ts +0 -3
  95. package/dist/helpers/security/index.d.ts.map +0 -1
  96. package/dist/helpers/security/index.js +0 -2
  97. package/dist/helpers/servers.d.ts +0 -24
  98. package/dist/helpers/servers.d.ts.map +0 -1
  99. package/dist/helpers/servers.js +0 -146
  100. package/dist/helpers/should-ignore-entity.d.ts +0 -8
  101. package/dist/helpers/should-ignore-entity.d.ts.map +0 -1
  102. package/dist/helpers/should-ignore-entity.js +0 -4
  103. package/dist/migrations/generate-types.d.ts +0 -2
  104. package/dist/migrations/generate-types.d.ts.map +0 -1
  105. package/dist/migrations/generate-types.js +0 -53
  106. package/dist/spec-getters/get-example-from-schema.d.ts +0 -36
  107. package/dist/spec-getters/get-example-from-schema.d.ts.map +0 -1
  108. package/dist/spec-getters/get-example-from-schema.js +0 -521
  109. package/dist/spec-getters/get-parameters-from-operation.d.ts +0 -17
  110. package/dist/spec-getters/get-parameters-from-operation.d.ts.map +0 -1
  111. package/dist/spec-getters/get-parameters-from-operation.js +0 -40
  112. package/dist/spec-getters/get-request-body-from-operation.d.ts +0 -14
  113. package/dist/spec-getters/get-request-body-from-operation.d.ts.map +0 -1
  114. package/dist/spec-getters/get-request-body-from-operation.js +0 -176
  115. package/dist/spec-getters/get-server-variable-examples.d.ts +0 -4
  116. package/dist/spec-getters/get-server-variable-examples.d.ts.map +0 -1
  117. package/dist/spec-getters/get-server-variable-examples.js +0 -12
  118. package/dist/spec-getters/index.d.ts +0 -5
  119. package/dist/spec-getters/index.d.ts.map +0 -1
  120. package/dist/spec-getters/index.js +0 -4
  121. package/dist/transforms/import-spec.d.ts +0 -64
  122. package/dist/transforms/import-spec.d.ts.map +0 -1
  123. package/dist/transforms/import-spec.js +0 -397
  124. package/dist/transforms/index.d.ts +0 -2
  125. package/dist/transforms/index.d.ts.map +0 -1
  126. package/dist/transforms/index.js +0 -1
@@ -1,449 +0,0 @@
1
- import { isDefined } from '@scalar/helpers/array/is-defined';
2
- import { objectKeys } from '@scalar/helpers/object/object-keys';
3
- import { keysOf } from '@scalar/object-utils/arrays';
4
- import { nanoidSchema } from '@scalar/types/utils';
5
- import { z } from 'zod';
6
- import { schemaModel } from '../../helpers/schema-model.js';
7
- import { getRequestBodyFromOperation } from '../../spec-getters/get-request-body-from-operation.js';
8
- import { getServerVariableExamples } from '../../spec-getters/get-server-variable-examples.js';
9
- // ---------------------------------------------------------------------------
10
- // Example Parameters
11
- /**
12
- * TODO: Deprecate this.
13
- *
14
- * The request schema should be stored in the request and any
15
- * parameters should be validated against that
16
- */
17
- export const requestExampleParametersSchema = z
18
- .object({
19
- key: z.string().default(''),
20
- value: z.coerce.string().default(''),
21
- enabled: z.boolean().default(true),
22
- file: z.any().optional(),
23
- description: z.string().optional(),
24
- required: z.boolean().optional(),
25
- enum: z.array(z.string()).optional(),
26
- examples: z.array(z.any()).optional(),
27
- type: z
28
- .union([
29
- // 'string'
30
- z.string(),
31
- // ['string', 'null']
32
- z.array(z.string()),
33
- ])
34
- .optional(),
35
- format: z.string().optional(),
36
- minimum: z.number().optional(),
37
- maximum: z.number().optional(),
38
- default: z.any().optional(),
39
- nullable: z.boolean().optional(),
40
- })
41
- // set nullable: to true if type is ['string', 'null']
42
- .transform((_data) => {
43
- const data = { ..._data };
44
- // type: ['string', 'null'] -> nullable: true
45
- if (Array.isArray(data.type) && data.type.includes('null')) {
46
- data.nullable = true;
47
- }
48
- // Hey, if it's just one value and 'null', we can make it a string and ditch the 'null'.
49
- if (Array.isArray(data.type) && data.type.length === 2 && data.type.includes('null')) {
50
- data.type = data.type.find((item) => item !== 'null');
51
- }
52
- return data;
53
- });
54
- /** Convert the array of parameters to an object keyed by the parameter name */
55
- export const parameterArrayToObject = (params) => params.reduce((map, param) => {
56
- map[param.key] = param.value;
57
- return map;
58
- }, {});
59
- const xScalarFileValueSchema = z
60
- .object({
61
- url: z.string(),
62
- base64: z.string().optional(),
63
- })
64
- .nullable();
65
- /**
66
- * When files are required for an example we provide the options
67
- * to provide a public URL or a base64 encoded string
68
- */
69
- // not used but kept for consistency
70
- // export type XScalarFileValue = z.infer<typeof xScalarFileValueSchema>
71
- /**
72
- * Schema for the OAS serialization of request example parameters
73
- *
74
- * File values can be optionally fetched on import OR inserted as a base64 encoded string
75
- */
76
- const xScalarFormDataValue = z.union([
77
- z.object({
78
- type: z.literal('string'),
79
- value: z.string(),
80
- }),
81
- z.object({
82
- type: z.literal('file'),
83
- file: xScalarFileValueSchema,
84
- }),
85
- ]);
86
- // ---------------------------------------------------------------------------
87
- // Example Body
88
- /**
89
- * Possible encodings for example request bodies when using text formats
90
- *
91
- * TODO: This list may not be comprehensive enough
92
- */
93
- const exampleRequestBodyEncoding = ['json', 'text', 'html', 'javascript', 'xml', 'yaml', 'edn'];
94
- const exampleBodyMime = [
95
- 'application/json',
96
- 'text/plain',
97
- 'text/html',
98
- 'application/javascript',
99
- 'application/xml',
100
- 'application/yaml',
101
- 'application/edn',
102
- 'application/octet-stream',
103
- 'application/x-www-form-urlencoded',
104
- 'multipart/form-data',
105
- /** Used for direct files */
106
- 'binary',
107
- ];
108
- const contentMapping = {
109
- json: 'application/json',
110
- text: 'text/plain',
111
- html: 'text/html',
112
- javascript: 'application/javascript',
113
- xml: 'application/xml',
114
- yaml: 'application/yaml',
115
- edn: 'application/edn',
116
- };
117
- /**
118
- * TODO: Migrate away from this layout to the format used in the extension
119
- *
120
- * If a user changes the encoding of the body we expect the content to change as well
121
- */
122
- const exampleRequestBodySchema = z.object({
123
- raw: z
124
- .object({
125
- encoding: z.enum(exampleRequestBodyEncoding),
126
- value: z.string().default(''),
127
- mimeType: z.string().optional(),
128
- })
129
- .optional(),
130
- formData: z
131
- .object({
132
- encoding: z.union([z.literal('form-data'), z.literal('urlencoded')]).default('form-data'),
133
- value: requestExampleParametersSchema.array().default([]),
134
- })
135
- .optional(),
136
- binary: z.instanceof(Blob).optional(),
137
- activeBody: z.union([z.literal('raw'), z.literal('formData'), z.literal('binary')]).default('raw'),
138
- });
139
- /** Schema for the OAS serialization of request example bodies */
140
- const xScalarExampleBodySchema = z.object({
141
- encoding: z.enum(exampleBodyMime),
142
- /**
143
- * Body content as an object with a separately specified encoding or a simple pre-encoded string value
144
- *
145
- * Ideally we would convert any objects into the proper encoding on import
146
- */
147
- content: z.union([z.record(z.string(), z.any()), z.string()]),
148
- /** When the encoding is `binary` this will be used to link to the file */
149
- file: xScalarFileValueSchema.optional(),
150
- });
151
- // ---------------------------------------------------------------------------
152
- // Example Schema
153
- export const requestExampleSchema = z.object({
154
- uid: nanoidSchema.brand(),
155
- type: z.literal('requestExample').optional().default('requestExample'),
156
- requestUid: z.string().brand().optional(),
157
- name: z.string().optional().default('Name'),
158
- body: exampleRequestBodySchema.optional().default({ activeBody: 'raw' }),
159
- parameters: z
160
- .object({
161
- path: requestExampleParametersSchema.array().default([]),
162
- query: requestExampleParametersSchema.array().default([]),
163
- headers: requestExampleParametersSchema.array().default([{ key: 'Accept', value: '*/*', enabled: true }]),
164
- cookies: requestExampleParametersSchema.array().default([]),
165
- })
166
- .optional()
167
- .default({
168
- path: [],
169
- query: [],
170
- headers: [{ key: 'Accept', value: '*/*', enabled: true }],
171
- cookies: [],
172
- }),
173
- /** TODO: Should this be deprecated? */
174
- serverVariables: z.record(z.string(), z.array(z.string())).optional(),
175
- });
176
- /** For OAS serialization we just store the simple key/value pairs */
177
- const xScalarExampleParameterSchema = z.record(z.string(), z.string()).optional();
178
- /** Schema for the OAS serialization of request examples */
179
- export const xScalarExampleSchema = z.object({
180
- /** TODO: Should this be required? */
181
- name: z.string().optional(),
182
- body: xScalarExampleBodySchema.optional(),
183
- parameters: z.object({
184
- path: xScalarExampleParameterSchema,
185
- query: xScalarExampleParameterSchema,
186
- headers: xScalarExampleParameterSchema,
187
- cookies: xScalarExampleParameterSchema,
188
- }),
189
- });
190
- /**
191
- * Convert a request example to the xScalar serialized format
192
- *
193
- * TODO: The base format should be migrated to align MUCH closer to the serialized format
194
- */
195
- export function convertExampleToXScalar(example) {
196
- const active = example.body?.activeBody;
197
- const xScalarBody = {
198
- encoding: 'text/plain',
199
- content: '',
200
- };
201
- if (example.body?.activeBody === 'binary') {
202
- xScalarBody.encoding = 'binary';
203
- // TODO: Need to allow users to set these properties
204
- xScalarBody.file = null;
205
- }
206
- if (active === 'formData' && example.body?.[active]) {
207
- const body = example.body[active];
208
- xScalarBody.encoding = body.encoding === 'form-data' ? 'multipart/form-data' : 'application/x-www-form-urlencoded';
209
- // TODO: Need to allow users to set these properties
210
- xScalarBody.content = body.value.reduce((map, param) => {
211
- /** TODO: We need to ensure only file or value is set */
212
- map[param.key] = param.file
213
- ? {
214
- type: 'file',
215
- file: null,
216
- }
217
- : {
218
- type: 'string',
219
- value: param.value,
220
- };
221
- return map;
222
- }, {});
223
- }
224
- if (example.body?.activeBody === 'raw') {
225
- xScalarBody.encoding = contentMapping[example.body.raw?.encoding ?? 'text'] ?? 'text/plain';
226
- xScalarBody.content = example.body.raw?.value ?? '';
227
- }
228
- const parameters = {};
229
- keysOf(example.parameters ?? {}).forEach((key) => {
230
- if (example.parameters?.[key].length) {
231
- parameters[key] = parameterArrayToObject(example.parameters[key]);
232
- }
233
- });
234
- return xScalarExampleSchema.parse({
235
- /** Only add the body if we have content or the body should be a file */
236
- body: xScalarBody.content || xScalarBody.encoding === 'binary' ? xScalarBody : undefined,
237
- parameters,
238
- });
239
- }
240
- // ---------------------------------------------------------------------------
241
- // Example Helpers
242
- /** Create new instance parameter from a request parameter */
243
- export function createParamInstance(param) {
244
- const schema = param.schema;
245
- const firstExample = (() => {
246
- if (param.examples && !Array.isArray(param.examples) && objectKeys(param.examples).length > 0) {
247
- const exampleValues = Object.entries(param.examples).map(([_, example]) => {
248
- // returns the external value if it exists
249
- if (example.externalValue) {
250
- return example.externalValue;
251
- }
252
- // returns the value if it exists and is defined
253
- // e.g. { examples: { foo: { value: 'bar' } } } would return ['bar']
254
- return example.value;
255
- });
256
- // returns the first example as selected value along other examples
257
- return { value: exampleValues[0], examples: exampleValues };
258
- }
259
- // param example e.g. { example: 'foo' }
260
- if (isDefined(param.example)) {
261
- return { value: param.example };
262
- }
263
- // param examples e.g. { examples: ['foo', 'bar'] }
264
- if (Array.isArray(param.examples) && param.examples.length > 0) {
265
- return { value: param.examples[0] };
266
- }
267
- // schema example e.g. { example: 'foo' } while being discouraged
268
- // see https://spec.openapis.org/oas/v3.1.1.html#fixed-fields-20
269
- if (isDefined(schema?.example)) {
270
- return { value: schema.example };
271
- }
272
- // schema examples e.g. { examples: ['foo', 'bar'] }
273
- if (Array.isArray(schema?.examples) && schema.examples.length > 0) {
274
- // For boolean type, default to false when using schema examples
275
- if (schema?.type === 'boolean') {
276
- return { value: schema.default ?? false };
277
- }
278
- return { value: schema.examples[0] };
279
- }
280
- // content examples e.g. { content: { 'application/json': { examples: { foo: { value: 'bar' } } } } }
281
- if (param.content) {
282
- const firstContentType = objectKeys(param.content)[0];
283
- if (firstContentType) {
284
- const content = param.content[firstContentType];
285
- if (content?.examples) {
286
- const firstExampleKey = Object.keys(content.examples)[0];
287
- if (firstExampleKey) {
288
- const example = content.examples[firstExampleKey];
289
- if (isDefined(example?.value)) {
290
- return { value: example.value };
291
- }
292
- }
293
- }
294
- // content example e.g. { example: 'foo' }
295
- if (isDefined(content?.example)) {
296
- return { value: content.example };
297
- }
298
- }
299
- }
300
- return null;
301
- })();
302
- /**
303
- * TODO:
304
- * - Need better value defaulting here
305
- * - Need to handle non-string parameters much better
306
- * - Need to handle unions/array values for schema
307
- */
308
- const value = String(firstExample?.value ?? schema?.default ?? '');
309
- // Handle non-string enums and enums within items for array types
310
- const parseEnum = (() => {
311
- if (schema?.enum && schema?.type !== 'string') {
312
- return schema.enum?.map(String);
313
- }
314
- if (schema?.items?.enum && schema?.type === 'array') {
315
- return schema.items.enum.map(String);
316
- }
317
- return schema?.enum;
318
- })();
319
- // Handle parameter examples
320
- const examples = firstExample?.examples ||
321
- (schema?.examples && schema?.type !== 'string' ? schema.examples?.map(String) : schema?.examples);
322
- // safe parse the example
323
- const example = schemaModel({
324
- ...schema,
325
- key: param.name,
326
- value,
327
- description: param.description,
328
- required: param.required,
329
- /** Initialized all required properties to enabled */
330
- enabled: !!param.required,
331
- enum: parseEnum,
332
- examples,
333
- }, requestExampleParametersSchema, false);
334
- if (!example) {
335
- console.warn(`Example at ${param.name} is invalid.`);
336
- return requestExampleParametersSchema.parse({});
337
- }
338
- return example;
339
- }
340
- /**
341
- * Create new request example from a request
342
- * Iterates the name of the example if provided
343
- */
344
- export function createExampleFromRequest(request, name, server) {
345
- // ---------------------------------------------------------------------------
346
- // Populate all parameters with an example value
347
- const parameters = {
348
- path: [],
349
- query: [],
350
- cookie: [],
351
- // deprecated TODO: add zod transform to remove
352
- header: [],
353
- headers: [{ key: 'Accept', value: '*/*', enabled: true }],
354
- };
355
- // Populated the separated params
356
- request.parameters?.forEach((p) => parameters[p.in].push(createParamInstance(p)));
357
- // TODO: add zod transform to remove header and only support headers
358
- if (parameters.header.length > 0) {
359
- parameters.headers = parameters.header;
360
- parameters.header = [];
361
- }
362
- // Get content type header
363
- const contentTypeHeader = parameters.headers.find((h) => h.key.toLowerCase() === 'content-type');
364
- // ---------------------------------------------------------------------------
365
- // Handle request body defaulting for various content type encodings
366
- const body = {
367
- activeBody: 'raw',
368
- };
369
- // If we have a request body or a content type header
370
- // TODO: we don't even handle path parameters here
371
- if (request.requestBody || contentTypeHeader?.value) {
372
- const requestBody = getRequestBodyFromOperation(request);
373
- const contentType = request.requestBody ? requestBody?.mimeType : contentTypeHeader?.value;
374
- // Handle JSON and JSON-like mimetypes
375
- if (contentType?.includes('/json') || contentType?.endsWith('+json')) {
376
- body.activeBody = 'raw';
377
- body.raw = {
378
- encoding: 'json',
379
- mimeType: contentType,
380
- value: requestBody?.text ?? JSON.stringify({}),
381
- };
382
- }
383
- if (contentType === 'application/xml') {
384
- body.activeBody = 'raw';
385
- body.raw = {
386
- encoding: 'xml',
387
- value: requestBody?.text ?? '',
388
- };
389
- }
390
- /**
391
- * TODO: Are we loading example files from somewhere based on the spec?
392
- * How are we handling the body values
393
- */
394
- if (contentType === 'application/octet-stream') {
395
- body.activeBody = 'binary';
396
- body.binary = undefined;
397
- }
398
- if (contentType === 'application/x-www-form-urlencoded' || contentType === 'multipart/form-data') {
399
- body.activeBody = 'formData';
400
- body.formData = {
401
- encoding: contentType === 'application/x-www-form-urlencoded' ? 'urlencoded' : 'form-data',
402
- value: (requestBody?.params || []).map((param) => {
403
- if (param.value instanceof File) {
404
- return {
405
- key: param.name,
406
- value: 'BINARY',
407
- file: param.value,
408
- enabled: true,
409
- };
410
- }
411
- return {
412
- key: param.name,
413
- value: param.value || '',
414
- enabled: true,
415
- };
416
- }),
417
- };
418
- }
419
- if (contentType?.startsWith('text/')) {
420
- body.activeBody = 'raw';
421
- body.raw = {
422
- encoding: 'text',
423
- value: requestBody?.text ?? '',
424
- };
425
- }
426
- // Add the content-type header if it doesn't exist and if it's not multipart request
427
- if (requestBody?.mimeType && !contentTypeHeader && !requestBody.mimeType.startsWith('multipart/')) {
428
- parameters.headers.push({
429
- key: 'Content-Type',
430
- value: requestBody.mimeType,
431
- enabled: true,
432
- });
433
- }
434
- }
435
- const serverVariables = server ? getServerVariableExamples(server) : {};
436
- // safe parse the example
437
- const example = schemaModel({
438
- requestUid: request.uid,
439
- parameters,
440
- name,
441
- body,
442
- serverVariables,
443
- }, requestExampleSchema, false);
444
- if (!example) {
445
- console.warn(`Example at ${request.uid} is invalid.`);
446
- return requestExampleSchema.parse({});
447
- }
448
- return example;
449
- }
@@ -1,137 +0,0 @@
1
- import { XScalarStability } from '@scalar/types/legacy';
2
- import type { XPostResponse } from '@scalar/workspace-store/schemas/extensions/general/x-post-response';
3
- import { z } from 'zod';
4
- import { type RequestExample } from './request-examples.js';
5
- /** The code to execute */
6
- declare const postResponseSchema: z.ZodString;
7
- declare const requestMethods: readonly ["delete", "get", "head", "options", "patch", "post", "put", "trace"];
8
- export type RequestMethod = (typeof requestMethods)[number];
9
- /** A single set of populated values for a sent request */
10
- export type ResponseInstance = Omit<Response, 'headers'> & {
11
- /** Store headers as an object to match what we had with axios */
12
- headers: Record<string, string>;
13
- /** Keys of headers which set cookies */
14
- cookieHeaderKeys: string[];
15
- /** Time in ms the request took */
16
- duration: number;
17
- /** The response status */
18
- status: number;
19
- /** The response status text */
20
- statusText: string;
21
- /** The response method */
22
- method: RequestMethod;
23
- /** The request path */
24
- path: string;
25
- } & ({
26
- /** The response data */
27
- data: string | Blob;
28
- /** The response size in bytes */
29
- size: number;
30
- } | {
31
- /** A stream reader for a streamable response body */
32
- reader: ReadableStreamDefaultReader<Uint8Array>;
33
- });
34
- /** A single request/response set to save to the history stack */
35
- export type RequestEvent = {
36
- request: RequestExample;
37
- response: ResponseInstance;
38
- timestamp: number;
39
- };
40
- export type PostResponseScript = z.infer<typeof postResponseSchema>;
41
- export type PostResponseScripts = XPostResponse['x-post-response'];
42
- /** Unified request schema for client usage */
43
- export declare const requestSchema: z.ZodObject<{
44
- description: z.ZodOptional<z.ZodString>;
45
- summary: z.ZodOptional<z.ZodString>;
46
- externalDocs: z.ZodOptional<z.ZodPipe<z.ZodObject<{
47
- description: z.ZodCatch<z.ZodOptional<z.ZodString>>;
48
- url: z.ZodString;
49
- }, z.core.$strip>, z.ZodTransform<{
50
- url: string;
51
- description?: string | undefined;
52
- }, {
53
- url: string;
54
- description?: string | undefined;
55
- }>>>;
56
- 'x-internal': z.ZodOptional<z.ZodBoolean>;
57
- 'x-scalar-ignore': z.ZodOptional<z.ZodBoolean>;
58
- security: z.ZodOptional<z.ZodArray<z.ZodRecord<z.ZodString, z.ZodDefault<z.ZodOptional<z.ZodArray<z.ZodString>>>>>>;
59
- tags: z.ZodOptional<z.ZodArray<z.ZodString>>;
60
- deprecated: z.ZodOptional<z.ZodBoolean>;
61
- operationId: z.ZodOptional<z.ZodString>;
62
- requestBody: z.ZodOptional<z.ZodAny>;
63
- parameters: z.ZodOptional<z.ZodArray<z.ZodObject<{
64
- in: z.ZodEnum<{
65
- cookie: "cookie";
66
- path: "path";
67
- query: "query";
68
- header: "header";
69
- }>;
70
- name: z.ZodString;
71
- description: z.ZodOptional<z.ZodString>;
72
- required: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
73
- deprecated: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
74
- schema: z.ZodOptional<z.ZodUnknown>;
75
- content: z.ZodOptional<z.ZodUnknown>;
76
- style: z.ZodOptional<z.ZodEnum<{
77
- matrix: "matrix";
78
- label: "label";
79
- form: "form";
80
- simple: "simple";
81
- spaceDelimited: "spaceDelimited";
82
- pipeDelimited: "pipeDelimited";
83
- deepObject: "deepObject";
84
- }>>;
85
- explode: z.ZodOptional<z.ZodBoolean>;
86
- example: z.ZodOptional<z.ZodUnknown>;
87
- examples: z.ZodOptional<z.ZodUnion<readonly [z.ZodRecord<z.ZodString, z.ZodObject<{
88
- value: z.ZodOptional<z.ZodUnknown>;
89
- summary: z.ZodOptional<z.ZodString>;
90
- externalValue: z.ZodOptional<z.ZodString>;
91
- }, z.core.$strip>>, z.ZodArray<z.ZodUnknown>]>>;
92
- }, z.core.$strip>>>;
93
- responses: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
94
- callbacks: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodRecord<z.ZodString, z.ZodRecord<z.ZodString, z.ZodAny>>>>;
95
- 'x-codeSamples': z.ZodCatch<z.ZodOptional<z.ZodArray<z.ZodObject<{
96
- lang: z.ZodCatch<z.ZodOptional<z.ZodString>>;
97
- label: z.ZodCatch<z.ZodOptional<z.ZodString>>;
98
- source: z.ZodString;
99
- }, z.core.$strip>>>>;
100
- 'x-code-samples': z.ZodCatch<z.ZodOptional<z.ZodArray<z.ZodObject<{
101
- lang: z.ZodCatch<z.ZodOptional<z.ZodString>>;
102
- label: z.ZodCatch<z.ZodOptional<z.ZodString>>;
103
- source: z.ZodString;
104
- }, z.core.$strip>>>>;
105
- 'x-custom-examples': z.ZodCatch<z.ZodOptional<z.ZodArray<z.ZodObject<{
106
- lang: z.ZodCatch<z.ZodOptional<z.ZodString>>;
107
- label: z.ZodCatch<z.ZodOptional<z.ZodString>>;
108
- source: z.ZodString;
109
- }, z.core.$strip>>>>;
110
- 'x-scalar-stability': z.ZodCatch<z.ZodOptional<z.ZodEnum<{
111
- deprecated: XScalarStability.Deprecated;
112
- experimental: XScalarStability.Experimental;
113
- stable: XScalarStability.Stable;
114
- }>>>;
115
- type: z.ZodDefault<z.ZodOptional<z.ZodLiteral<"request">>>;
116
- uid: z.core.$ZodBranded<z.ZodDefault<z.ZodString>, "operation", "out">;
117
- path: z.ZodDefault<z.ZodOptional<z.ZodString>>;
118
- method: z.ZodDefault<z.ZodEnum<{
119
- delete: "delete";
120
- get: "get";
121
- head: "head";
122
- options: "options";
123
- patch: "patch";
124
- post: "post";
125
- put: "put";
126
- trace: "trace";
127
- }>>;
128
- servers: z.ZodDefault<z.ZodArray<z.core.$ZodBranded<z.ZodString, "server", "out">>>;
129
- selectedServerUid: z.ZodDefault<z.ZodNullable<z.ZodOptional<z.core.$ZodBranded<z.ZodString, "server", "out">>>>;
130
- examples: z.ZodDefault<z.ZodArray<z.core.$ZodBranded<z.ZodString, "example", "out">>>;
131
- selectedSecuritySchemeUids: z.ZodDefault<z.ZodArray<z.ZodUnion<readonly [z.core.$ZodBranded<z.ZodString, "securityScheme", "out">, z.ZodArray<z.core.$ZodBranded<z.ZodString, "securityScheme", "out">>]>>>;
132
- 'x-post-response': z.ZodOptional<z.ZodString>;
133
- }, z.core.$strip>;
134
- export type Request = z.infer<typeof requestSchema>;
135
- export type RequestPayload = z.input<typeof requestSchema>;
136
- export {};
137
- //# sourceMappingURL=requests.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"requests.d.ts","sourceRoot":"","sources":["../../../src/entities/spec/requests.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AAEvD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oEAAoE,CAAA;AAGvG,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAKvB,OAAO,EAAE,KAAK,cAAc,EAAwB,MAAM,oBAAoB,CAAA;AAe9E,0BAA0B;AAC1B,QAAA,MAAM,kBAAkB,aAAa,CAAA;AAMrC,QAAA,MAAM,cAAc,gFAAiF,CAAA;AAErG,MAAM,MAAM,aAAa,GAAG,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,CAAC,CAAA;AAE3D,0DAA0D;AAC1D,MAAM,MAAM,gBAAgB,GAAG,IAAI,CAAC,QAAQ,EAAE,SAAS,CAAC,GAAG;IACzD,iEAAiE;IACjE,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC/B,wCAAwC;IACxC,gBAAgB,EAAE,MAAM,EAAE,CAAA;IAC1B,kCAAkC;IAClC,QAAQ,EAAE,MAAM,CAAA;IAChB,0BAA0B;IAC1B,MAAM,EAAE,MAAM,CAAA;IACd,+BAA+B;IAC/B,UAAU,EAAE,MAAM,CAAA;IAClB,0BAA0B;IAC1B,MAAM,EAAE,aAAa,CAAA;IACrB,uBAAuB;IACvB,IAAI,EAAE,MAAM,CAAA;CACb,GAAG,CACE;IACE,wBAAwB;IACxB,IAAI,EAAE,MAAM,GAAG,IAAI,CAAA;IACnB,iCAAiC;IACjC,IAAI,EAAE,MAAM,CAAA;CACb,GACD;IACE,qDAAqD;IACrD,MAAM,EAAE,2BAA2B,CAAC,UAAU,CAAC,CAAA;CAChD,CACJ,CAAA;AAEH,iEAAiE;AACjE,MAAM,MAAM,YAAY,GAAG;IACzB,OAAO,EAAE,cAAc,CAAA;IACvB,QAAQ,EAAE,gBAAgB,CAAA;IAC1B,SAAS,EAAE,MAAM,CAAA;CAClB,CAAA;AAoGD,MAAM,MAAM,kBAAkB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,kBAAkB,CAAC,CAAA;AACnE,MAAM,MAAM,mBAAmB,GAAG,aAAa,CAAC,iBAAiB,CAAC,CAAA;AAElE,8CAA8C;AAC9C,eAAO,MAAM,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAKG,CAAA;AAE7B,MAAM,MAAM,OAAO,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,aAAa,CAAC,CAAA;AACnD,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,aAAa,CAAC,CAAA"}