@scalar/postman-to-openapi 0.5.2 → 0.6.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 (69) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/convert.d.ts +29 -1
  3. package/dist/convert.d.ts.map +1 -1
  4. package/dist/convert.js +375 -215
  5. package/dist/helpers/auth.js +116 -92
  6. package/dist/helpers/contact.js +24 -20
  7. package/dist/helpers/external-docs.js +33 -25
  8. package/dist/helpers/form-data.js +42 -36
  9. package/dist/helpers/generate-unique-value.d.ts +23 -0
  10. package/dist/helpers/generate-unique-value.d.ts.map +1 -0
  11. package/dist/helpers/generate-unique-value.js +29 -0
  12. package/dist/helpers/get-operation-examples.d.ts +40 -0
  13. package/dist/helpers/get-operation-examples.d.ts.map +1 -0
  14. package/dist/helpers/get-operation-examples.js +76 -0
  15. package/dist/helpers/license.js +21 -17
  16. package/dist/helpers/logo.js +22 -21
  17. package/dist/helpers/markdown.js +33 -30
  18. package/dist/helpers/merge-operation.d.ts +55 -0
  19. package/dist/helpers/merge-operation.d.ts.map +1 -0
  20. package/dist/helpers/merge-operation.js +125 -0
  21. package/dist/helpers/merge-path-item.d.ts +5 -0
  22. package/dist/helpers/merge-path-item.d.ts.map +1 -0
  23. package/dist/helpers/merge-path-item.js +37 -0
  24. package/dist/helpers/parameters.d.ts +2 -2
  25. package/dist/helpers/parameters.d.ts.map +1 -1
  26. package/dist/helpers/parameters.js +124 -96
  27. package/dist/helpers/path-items.d.ts +1 -1
  28. package/dist/helpers/path-items.d.ts.map +1 -1
  29. package/dist/helpers/path-items.js +245 -202
  30. package/dist/helpers/post-response-scripts.js +12 -12
  31. package/dist/helpers/pre-request-scripts.js +12 -12
  32. package/dist/helpers/prune-document.js +42 -35
  33. package/dist/helpers/rename-operation-example.d.ts +40 -0
  34. package/dist/helpers/rename-operation-example.d.ts.map +1 -0
  35. package/dist/helpers/rename-operation-example.js +61 -0
  36. package/dist/helpers/request-body.d.ts +1 -1
  37. package/dist/helpers/request-body.d.ts.map +1 -1
  38. package/dist/helpers/request-body.js +118 -94
  39. package/dist/helpers/responses.js +62 -57
  40. package/dist/helpers/schemas.js +43 -37
  41. package/dist/helpers/servers.js +83 -57
  42. package/dist/helpers/status-codes.js +40 -30
  43. package/dist/helpers/urls.js +74 -51
  44. package/dist/index.d.ts +2 -1
  45. package/dist/index.d.ts.map +1 -1
  46. package/dist/index.js +2 -5
  47. package/dist/types.js +1 -1
  48. package/package.json +7 -11
  49. package/dist/convert.js.map +0 -7
  50. package/dist/helpers/auth.js.map +0 -7
  51. package/dist/helpers/contact.js.map +0 -7
  52. package/dist/helpers/external-docs.js.map +0 -7
  53. package/dist/helpers/form-data.js.map +0 -7
  54. package/dist/helpers/license.js.map +0 -7
  55. package/dist/helpers/logo.js.map +0 -7
  56. package/dist/helpers/markdown.js.map +0 -7
  57. package/dist/helpers/parameters.js.map +0 -7
  58. package/dist/helpers/path-items.js.map +0 -7
  59. package/dist/helpers/post-response-scripts.js.map +0 -7
  60. package/dist/helpers/pre-request-scripts.js.map +0 -7
  61. package/dist/helpers/prune-document.js.map +0 -7
  62. package/dist/helpers/request-body.js.map +0 -7
  63. package/dist/helpers/responses.js.map +0 -7
  64. package/dist/helpers/schemas.js.map +0 -7
  65. package/dist/helpers/servers.js.map +0 -7
  66. package/dist/helpers/status-codes.js.map +0 -7
  67. package/dist/helpers/urls.js.map +0 -7
  68. package/dist/index.js.map +0 -7
  69. package/dist/types.js.map +0 -7
@@ -1,113 +1,137 @@
1
+ // Constants for security scheme names and URLs
1
2
  const AUTH_SCHEMES = {
2
- API_KEY: "apikeyAuth",
3
- BASIC: "basicAuth",
4
- BEARER: "bearerAuth",
5
- OAUTH2: "oauth2Auth"
3
+ API_KEY: 'apikeyAuth',
4
+ BASIC: 'basicAuth',
5
+ BEARER: 'bearerAuth',
6
+ OAUTH2: 'oauth2Auth',
6
7
  };
7
8
  const OAUTH2_DEFAULTS = {
8
- AUTHORIZE_URL: "/oauth/authorize",
9
- TOKEN_URL: "/oauth/token"
9
+ AUTHORIZE_URL: '/oauth/authorize',
10
+ TOKEN_URL: '/oauth/token',
10
11
  };
12
+ /**
13
+ * Creates security configuration for API key authentication
14
+ */
11
15
  function createApiKeyConfig() {
12
- return {
13
- scheme: {
14
- type: "apiKey",
15
- name: "api_key",
16
- in: "header"
17
- },
18
- requirement: { [AUTH_SCHEMES.API_KEY]: [] }
19
- };
16
+ return {
17
+ scheme: {
18
+ type: 'apiKey',
19
+ name: 'api_key',
20
+ in: 'header',
21
+ },
22
+ requirement: { [AUTH_SCHEMES.API_KEY]: [] },
23
+ };
20
24
  }
25
+ /**
26
+ * Creates security configuration for Basic authentication
27
+ */
21
28
  function createBasicConfig() {
22
- return {
23
- scheme: {
24
- type: "http",
25
- scheme: "basic"
26
- },
27
- requirement: { [AUTH_SCHEMES.BASIC]: [] }
28
- };
29
+ return {
30
+ scheme: {
31
+ type: 'http',
32
+ scheme: 'basic',
33
+ },
34
+ requirement: { [AUTH_SCHEMES.BASIC]: [] },
35
+ };
29
36
  }
37
+ /**
38
+ * Creates security configuration for Bearer token authentication
39
+ */
30
40
  function createBearerConfig() {
31
- return {
32
- scheme: {
33
- type: "http",
34
- scheme: "bearer"
35
- },
36
- requirement: { [AUTH_SCHEMES.BEARER]: [] }
37
- };
38
- }
39
- function createOAuth2Config(auth) {
40
- if (!auth) {
41
41
  return {
42
- scheme: {},
43
- requirement: {}
42
+ scheme: {
43
+ type: 'http',
44
+ scheme: 'bearer',
45
+ },
46
+ requirement: { [AUTH_SCHEMES.BEARER]: [] },
44
47
  };
45
- }
46
- const oauth2Attrs = auth.oauth2 || [];
47
- const attrMap = /* @__PURE__ */ new Map();
48
- oauth2Attrs.forEach((attr) => {
49
- if (attr.key && attr.value !== void 0) {
50
- attrMap.set(attr.key, String(attr.value));
48
+ }
49
+ /**
50
+ * Creates security configuration for OAuth2 authentication
51
+ * Extracts actual OAuth2 URLs and scopes from the Postman auth object
52
+ */
53
+ function createOAuth2Config(auth) {
54
+ if (!auth) {
55
+ return {
56
+ scheme: {},
57
+ requirement: {},
58
+ };
51
59
  }
52
- });
53
- const authUrl = attrMap.get("authUrl") || attrMap.get("authorizationUrl") || OAUTH2_DEFAULTS.AUTHORIZE_URL;
54
- const tokenUrl = attrMap.get("accessTokenUrl") || attrMap.get("tokenUrl") || OAUTH2_DEFAULTS.TOKEN_URL;
55
- const scopeValue = attrMap.get("scope") || "";
56
- const scopes = {};
57
- if (scopeValue) {
58
- const scopeList = scopeValue.split(/[,\s]+/).filter(Boolean);
59
- scopeList.forEach((scope) => {
60
- scopes[scope] = scope;
61
- });
62
- }
63
- return {
64
- scheme: {
65
- type: "oauth2",
66
- flows: {
67
- authorizationCode: {
68
- authorizationUrl: authUrl,
69
- tokenUrl,
70
- scopes
60
+ const oauth2Attrs = auth.oauth2 || [];
61
+ const attrMap = new Map();
62
+ oauth2Attrs.forEach((attr) => {
63
+ if (attr.key && attr.value !== undefined) {
64
+ attrMap.set(attr.key, String(attr.value));
71
65
  }
72
- }
73
- },
74
- requirement: { [AUTH_SCHEMES.OAUTH2]: Object.keys(scopes) }
75
- };
66
+ });
67
+ const authUrl = attrMap.get('authUrl') || attrMap.get('authorizationUrl') || OAUTH2_DEFAULTS.AUTHORIZE_URL;
68
+ const tokenUrl = attrMap.get('accessTokenUrl') || attrMap.get('tokenUrl') || OAUTH2_DEFAULTS.TOKEN_URL;
69
+ const scopeValue = attrMap.get('scope') || '';
70
+ // Parse scopes from the scope value (can be space or comma separated)
71
+ const scopes = {};
72
+ if (scopeValue) {
73
+ const scopeList = scopeValue.split(/[,\s]+/).filter(Boolean);
74
+ scopeList.forEach((scope) => {
75
+ scopes[scope] = scope;
76
+ });
77
+ }
78
+ return {
79
+ scheme: {
80
+ type: 'oauth2',
81
+ flows: {
82
+ authorizationCode: {
83
+ authorizationUrl: authUrl,
84
+ tokenUrl,
85
+ scopes,
86
+ },
87
+ },
88
+ },
89
+ requirement: { [AUTH_SCHEMES.OAUTH2]: Object.keys(scopes) },
90
+ };
76
91
  }
92
+ /**
93
+ * Creates security configuration for no authentication
94
+ */
77
95
  function createNoAuthConfig() {
78
- return {
79
- scheme: {},
80
- requirement: {}
81
- };
96
+ return {
97
+ scheme: {},
98
+ requirement: {},
99
+ };
82
100
  }
101
+ /**
102
+ * Maps authentication types to their configuration creators
103
+ */
83
104
  const AUTH_TYPE_HANDLERS = {
84
- apikey: createApiKeyConfig,
85
- basic: createBasicConfig,
86
- bearer: createBearerConfig,
87
- oauth2: createOAuth2Config,
88
- noauth: createNoAuthConfig
105
+ apikey: createApiKeyConfig,
106
+ basic: createBasicConfig,
107
+ bearer: createBearerConfig,
108
+ oauth2: createOAuth2Config,
109
+ noauth: createNoAuthConfig,
89
110
  };
90
- function processAuth(auth) {
91
- const securitySchemes = {};
92
- const security = [];
93
- try {
94
- const handler = AUTH_TYPE_HANDLERS[auth.type];
95
- if (!handler) {
96
- throw new Error(`Unsupported authentication type: ${auth.type}`);
111
+ /**
112
+ * Processes authentication information from a Postman collection and updates
113
+ * the OpenAPI document with the corresponding security schemes and requirements.
114
+ * Supports API key, basic auth, bearer token, OAuth2, and no authentication types.
115
+ */
116
+ export function processAuth(auth) {
117
+ const securitySchemes = {};
118
+ const security = [];
119
+ try {
120
+ const handler = AUTH_TYPE_HANDLERS[auth.type];
121
+ if (!handler) {
122
+ throw new Error(`Unsupported authentication type: ${auth.type}`);
123
+ }
124
+ const { scheme, requirement } = handler(auth);
125
+ // Only add security schemes and requirements if they're not empty
126
+ if (Object.keys(scheme).length > 0) {
127
+ const schemeKey = `${auth.type}Auth`;
128
+ securitySchemes[schemeKey] = scheme;
129
+ security.push(requirement);
130
+ }
97
131
  }
98
- const { scheme, requirement } = handler(auth);
99
- if (Object.keys(scheme).length > 0) {
100
- const schemeKey = `${auth.type}Auth`;
101
- securitySchemes[schemeKey] = scheme;
102
- security.push(requirement);
132
+ catch (error) {
133
+ console.error('Error processing authentication:', error);
134
+ throw error;
103
135
  }
104
- } catch (error) {
105
- console.error("Error processing authentication:", error);
106
- throw error;
107
- }
108
- return { securitySchemes, security };
136
+ return { securitySchemes, security };
109
137
  }
110
- export {
111
- processAuth
112
- };
113
- //# sourceMappingURL=auth.js.map
@@ -1,25 +1,29 @@
1
+ // Constants for contact variable keys
1
2
  const VARIABLE_KEYS = {
2
- NAME: "contact.name",
3
- URL: "contact.url",
4
- EMAIL: "contact.email"
3
+ NAME: 'contact.name',
4
+ URL: 'contact.url',
5
+ EMAIL: 'contact.email',
5
6
  };
7
+ /**
8
+ * Finds a specific variable in the collection by its key
9
+ */
6
10
  function findVariable(collection, key) {
7
- return collection.variable?.find((v) => v.key === key);
11
+ return collection.variable?.find((v) => v.key === key);
8
12
  }
9
- function processContact(collection) {
10
- const nameVar = findVariable(collection, VARIABLE_KEYS.NAME);
11
- const urlVar = findVariable(collection, VARIABLE_KEYS.URL);
12
- const emailVar = findVariable(collection, VARIABLE_KEYS.EMAIL);
13
- if (!nameVar?.value && !urlVar?.value && !emailVar?.value) {
14
- return void 0;
15
- }
16
- return {
17
- ...nameVar?.value && typeof nameVar.value === "string" && { name: nameVar.value },
18
- ...urlVar?.value && typeof urlVar.value === "string" && { url: urlVar.value },
19
- ...emailVar?.value && typeof emailVar.value === "string" && { email: emailVar.value }
20
- };
13
+ /**
14
+ * Processes contact information from collection variables.
15
+ * Returns an OpenAPI Contact Object if at least one contact field is present.
16
+ */
17
+ export function processContact(collection) {
18
+ const nameVar = findVariable(collection, VARIABLE_KEYS.NAME);
19
+ const urlVar = findVariable(collection, VARIABLE_KEYS.URL);
20
+ const emailVar = findVariable(collection, VARIABLE_KEYS.EMAIL);
21
+ if (!nameVar?.value && !urlVar?.value && !emailVar?.value) {
22
+ return undefined;
23
+ }
24
+ return {
25
+ ...(nameVar?.value && typeof nameVar.value === 'string' && { name: nameVar.value }),
26
+ ...(urlVar?.value && typeof urlVar.value === 'string' && { url: urlVar.value }),
27
+ ...(emailVar?.value && typeof emailVar.value === 'string' && { email: emailVar.value }),
28
+ };
21
29
  }
22
- export {
23
- processContact
24
- };
25
- //# sourceMappingURL=contact.js.map
@@ -1,32 +1,40 @@
1
+ // Constants for variable keys
1
2
  const VARIABLE_KEYS = {
2
- URL: "externalDocs.url",
3
- DESCRIPTION: "externalDocs.description"
3
+ URL: 'externalDocs.url',
4
+ DESCRIPTION: 'externalDocs.description',
4
5
  };
6
+ /**
7
+ * Finds a specific variable in the collection by its key
8
+ */
5
9
  function findVariable(collection, key) {
6
- return collection.variable?.find((v) => v.key === key);
10
+ return collection.variable?.find((v) => v.key === key);
7
11
  }
8
- function processExternalDocs(collection) {
9
- try {
10
- const urlVariable = findVariable(collection, VARIABLE_KEYS.URL);
11
- const descriptionVariable = findVariable(collection, VARIABLE_KEYS.DESCRIPTION);
12
- if (!urlVariable?.value) {
13
- return void 0;
12
+ /**
13
+ * Processes the external documentation information from a Postman Collection.
14
+ * This function checks for 'externalDocs.url' and 'externalDocs.description'
15
+ * in the collection variables and creates an OpenAPI External Documentation Object
16
+ * if the URL is present.
17
+ */
18
+ export function processExternalDocs(collection) {
19
+ try {
20
+ const urlVariable = findVariable(collection, VARIABLE_KEYS.URL);
21
+ const descriptionVariable = findVariable(collection, VARIABLE_KEYS.DESCRIPTION);
22
+ if (!urlVariable?.value) {
23
+ return undefined;
24
+ }
25
+ if (typeof urlVariable.value !== 'string') {
26
+ throw new Error('External docs URL must be a string');
27
+ }
28
+ return {
29
+ url: urlVariable.value,
30
+ ...(descriptionVariable?.value &&
31
+ typeof descriptionVariable.value === 'string' && {
32
+ description: descriptionVariable.value,
33
+ }),
34
+ };
14
35
  }
15
- if (typeof urlVariable.value !== "string") {
16
- throw new Error("External docs URL must be a string");
36
+ catch (error) {
37
+ console.error('Error processing external docs:', error);
38
+ throw error;
17
39
  }
18
- return {
19
- url: urlVariable.value,
20
- ...descriptionVariable?.value && typeof descriptionVariable.value === "string" && {
21
- description: descriptionVariable.value
22
- }
23
- };
24
- } catch (error) {
25
- console.error("Error processing external docs:", error);
26
- throw error;
27
- }
28
40
  }
29
- export {
30
- processExternalDocs
31
- };
32
- //# sourceMappingURL=external-docs.js.map
@@ -1,39 +1,45 @@
1
- function processFormDataSchema(formdata) {
2
- const schema = {
3
- type: "object",
4
- properties: {},
5
- required: []
6
- };
7
- formdata.forEach((item) => {
8
- if (!schema.properties) {
9
- return;
10
- }
11
- const property = {
12
- type: "string"
1
+ /**
2
+ * Processes form data parameters from a Postman request and converts them into an OpenAPI schema.
3
+ * Handles file uploads, required fields, and descriptions.
4
+ */
5
+ export function processFormDataSchema(formdata) {
6
+ const schema = {
7
+ type: 'object',
8
+ properties: {},
9
+ required: [],
13
10
  };
14
- if (item.description) {
15
- const descriptionText = typeof item.description === "string" ? item.description : item.description.content || "";
16
- property.description = descriptionText.replace(" [required]", "");
17
- if (descriptionText.includes("[required]")) {
18
- schema.required?.push(item.key);
19
- }
20
- }
21
- if (item.type === "file") {
22
- property.format = "binary";
23
- } else {
24
- property.example = item.value;
25
- }
26
- if (item.disabled === true) {
27
- property["x-scalar-disabled"] = true;
11
+ formdata.forEach((item) => {
12
+ if (!schema.properties) {
13
+ return;
14
+ }
15
+ const property = {
16
+ type: 'string',
17
+ };
18
+ // Add description if present, handling both string and object descriptions
19
+ if (item.description) {
20
+ const descriptionText = typeof item.description === 'string' ? item.description : item.description.content || '';
21
+ property.description = descriptionText.replace(' [required]', '');
22
+ // If [required] was present, add to required array
23
+ if (descriptionText.includes('[required]')) {
24
+ schema.required?.push(item.key);
25
+ }
26
+ }
27
+ // Handle file type fields
28
+ if (item.type === 'file') {
29
+ property.format = 'binary';
30
+ }
31
+ else {
32
+ property.example = item.value;
33
+ }
34
+ // Add x-scalar-disabled extension if parameter is disabled
35
+ if (item.disabled === true) {
36
+ property['x-scalar-disabled'] = true;
37
+ }
38
+ schema.properties[item.key] = property;
39
+ });
40
+ // Only keep required array if it has items
41
+ if (schema.required?.length === 0) {
42
+ delete schema.required;
28
43
  }
29
- schema.properties[item.key] = property;
30
- });
31
- if (schema.required?.length === 0) {
32
- delete schema.required;
33
- }
34
- return schema;
44
+ return schema;
35
45
  }
36
- export {
37
- processFormDataSchema
38
- };
39
- //# sourceMappingURL=form-data.js.map
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Generates a unique string value based on a default value and a validation function.
3
+ * The function tries the default value first. If validation fails, it appends
4
+ * an incrementing suffix (optionally with a prefix) until the validation passes.
5
+ *
6
+ * @param defaultValue - The initial base string value to try.
7
+ * @param validation - A function that receives a candidate value and returns true if it is valid.
8
+ * @param prefix - Optional prefix to add before the incrementing number (default is '').
9
+ * @returns The first value that passes the validation.
10
+ *
11
+ * @example
12
+ * // Case 1: Simple unused value
13
+ * generateUniqueValue('example', v => v !== 'example') // → "example 2"
14
+ *
15
+ * // Case 2: Avoids taken values in a set
16
+ * const taken = new Set(['foo', 'foo 1', 'foo 2']);
17
+ * generateUniqueValue('foo', v => !taken.has(v)) // → "foo 3"
18
+ *
19
+ * // Case 3: Using a prefix
20
+ * generateUniqueValue('base', v => v === 'base__3', '__') // → "base__3"
21
+ */
22
+ export declare const generateUniqueValue: (defaultValue: string, validation: (value: string) => boolean, prefix?: string) => string;
23
+ //# sourceMappingURL=generate-unique-value.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generate-unique-value.d.ts","sourceRoot":"","sources":["../../src/helpers/generate-unique-value.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,mBAAmB,GAC9B,cAAc,MAAM,EACpB,YAAY,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,EACtC,eAAW,KACV,MAOF,CAAA"}
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Generates a unique string value based on a default value and a validation function.
3
+ * The function tries the default value first. If validation fails, it appends
4
+ * an incrementing suffix (optionally with a prefix) until the validation passes.
5
+ *
6
+ * @param defaultValue - The initial base string value to try.
7
+ * @param validation - A function that receives a candidate value and returns true if it is valid.
8
+ * @param prefix - Optional prefix to add before the incrementing number (default is '').
9
+ * @returns The first value that passes the validation.
10
+ *
11
+ * @example
12
+ * // Case 1: Simple unused value
13
+ * generateUniqueValue('example', v => v !== 'example') // → "example 2"
14
+ *
15
+ * // Case 2: Avoids taken values in a set
16
+ * const taken = new Set(['foo', 'foo 1', 'foo 2']);
17
+ * generateUniqueValue('foo', v => !taken.has(v)) // → "foo 3"
18
+ *
19
+ * // Case 3: Using a prefix
20
+ * generateUniqueValue('base', v => v === 'base__3', '__') // → "base__3"
21
+ */
22
+ export const generateUniqueValue = (defaultValue, validation, prefix = '') => {
23
+ let i = 1;
24
+ let value = defaultValue;
25
+ while (!validation(value)) {
26
+ value = `${defaultValue} ${prefix}${i++}`;
27
+ }
28
+ return value;
29
+ };
@@ -0,0 +1,40 @@
1
+ import type { OpenAPIV3_1 } from '@scalar/openapi-types';
2
+ /**
3
+ * Collects all example names used on parameters and requestBodies
4
+ * within all operations of a PathItemObject.
5
+ *
6
+ * This is useful for checking which example names are already present
7
+ * so you can avoid clashes (e.g., generating unique example names
8
+ * when merging path items).
9
+ *
10
+ * @param path - The OpenAPI PathItemObject to scan
11
+ * @returns Set of all unique example names found across parameters and requestBodies
12
+ *
13
+ * @example
14
+ * const operation: OpenAPIV3_1.OperationObject = {
15
+ * parameters: [
16
+ * {
17
+ * name: 'id',
18
+ * in: 'query',
19
+ * examples: {
20
+ * foo: { value: 'bar' },
21
+ * bar: { value: 'baz' }
22
+ * }
23
+ * }
24
+ * ],
25
+ * requestBody: {
26
+ * content: {
27
+ * 'application/json': {
28
+ * examples: {
29
+ * default: { value: 1 },
30
+ * extra: { value: 2 }
31
+ * }
32
+ * }
33
+ * }
34
+ * }
35
+ * }
36
+ * const path = { get: operation }
37
+ * // getOperationExamples(path) => Set { 'foo', 'bar', 'default', 'extra' }
38
+ */
39
+ export declare const getOperationExamples: (path: OpenAPIV3_1.PathItemObject) => Set<string>;
40
+ //# sourceMappingURL=get-operation-examples.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-operation-examples.d.ts","sourceRoot":"","sources":["../../src/helpers/get-operation-examples.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAExD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,eAAO,MAAM,oBAAoB,GAAI,MAAM,WAAW,CAAC,cAAc,gBA2CpE,CAAA"}
@@ -0,0 +1,76 @@
1
+ import { HTTP_METHODS } from '@scalar/helpers/http/http-methods';
2
+ /**
3
+ * Collects all example names used on parameters and requestBodies
4
+ * within all operations of a PathItemObject.
5
+ *
6
+ * This is useful for checking which example names are already present
7
+ * so you can avoid clashes (e.g., generating unique example names
8
+ * when merging path items).
9
+ *
10
+ * @param path - The OpenAPI PathItemObject to scan
11
+ * @returns Set of all unique example names found across parameters and requestBodies
12
+ *
13
+ * @example
14
+ * const operation: OpenAPIV3_1.OperationObject = {
15
+ * parameters: [
16
+ * {
17
+ * name: 'id',
18
+ * in: 'query',
19
+ * examples: {
20
+ * foo: { value: 'bar' },
21
+ * bar: { value: 'baz' }
22
+ * }
23
+ * }
24
+ * ],
25
+ * requestBody: {
26
+ * content: {
27
+ * 'application/json': {
28
+ * examples: {
29
+ * default: { value: 1 },
30
+ * extra: { value: 2 }
31
+ * }
32
+ * }
33
+ * }
34
+ * }
35
+ * }
36
+ * const path = { get: operation }
37
+ * // getOperationExamples(path) => Set { 'foo', 'bar', 'default', 'extra' }
38
+ */
39
+ export const getOperationExamples = (path) => {
40
+ const exampleNames = new Set();
41
+ for (const key of HTTP_METHODS) {
42
+ const operation = path[key];
43
+ if (operation === undefined || typeof operation !== 'object') {
44
+ continue;
45
+ }
46
+ const op = operation;
47
+ if ('parameters' in op) {
48
+ op.parameters?.forEach((parameter) => {
49
+ if (parameter.examples) {
50
+ for (const exampleName of Object.keys(parameter.examples)) {
51
+ exampleNames.add(exampleName);
52
+ }
53
+ }
54
+ });
55
+ }
56
+ if ('requestBody' in op) {
57
+ const requestBody = op.requestBody;
58
+ if (requestBody?.content) {
59
+ for (const mediaTypeObject of Object.values(requestBody.content)) {
60
+ const examples = mediaTypeObject &&
61
+ typeof mediaTypeObject === 'object' &&
62
+ 'examples' in mediaTypeObject &&
63
+ typeof mediaTypeObject.examples === 'object'
64
+ ? mediaTypeObject.examples
65
+ : undefined;
66
+ if (examples) {
67
+ for (const exampleName of Object.keys(examples)) {
68
+ exampleNames.add(exampleName);
69
+ }
70
+ }
71
+ }
72
+ }
73
+ }
74
+ }
75
+ return exampleNames;
76
+ };
@@ -1,22 +1,26 @@
1
+ // Constants for license variable keys
1
2
  const VARIABLE_KEYS = {
2
- NAME: "license.name",
3
- URL: "license.url"
3
+ NAME: 'license.name',
4
+ URL: 'license.url',
4
5
  };
6
+ /**
7
+ * Finds a specific variable in the collection by its key
8
+ */
5
9
  function findVariable(collection, key) {
6
- return collection.variable?.find((v) => v.key === key);
10
+ return collection.variable?.find((v) => v.key === key);
7
11
  }
8
- function processLicense(collection) {
9
- const nameVar = findVariable(collection, VARIABLE_KEYS.NAME);
10
- if (!nameVar?.value || typeof nameVar.value !== "string") {
11
- return void 0;
12
- }
13
- const urlVar = findVariable(collection, VARIABLE_KEYS.URL);
14
- return {
15
- name: nameVar.value,
16
- ...urlVar?.value && typeof urlVar.value === "string" && { url: urlVar.value }
17
- };
12
+ /**
13
+ * Processes license information from collection variables.
14
+ * Returns an OpenAPI License Object if the license name is present.
15
+ */
16
+ export function processLicense(collection) {
17
+ const nameVar = findVariable(collection, VARIABLE_KEYS.NAME);
18
+ if (!nameVar?.value || typeof nameVar.value !== 'string') {
19
+ return undefined;
20
+ }
21
+ const urlVar = findVariable(collection, VARIABLE_KEYS.URL);
22
+ return {
23
+ name: nameVar.value,
24
+ ...(urlVar?.value && typeof urlVar.value === 'string' && { url: urlVar.value }),
25
+ };
18
26
  }
19
- export {
20
- processLicense
21
- };
22
- //# sourceMappingURL=license.js.map