@scalar/workspace-store 0.23.0 → 0.24.1
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 +67 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +3 -2
- package/dist/client.js.map +2 -2
- package/dist/events/definitions/hooks.d.ts +25 -0
- package/dist/events/definitions/hooks.d.ts.map +1 -0
- package/dist/events/definitions/hooks.js +1 -0
- package/dist/events/definitions/hooks.js.map +7 -0
- package/dist/events/definitions/index.d.ts +2 -1
- package/dist/events/definitions/index.d.ts.map +1 -1
- package/dist/events/definitions/operation.d.ts +5 -5
- package/dist/events/definitions/operation.d.ts.map +1 -1
- package/dist/events/definitions/ui.d.ts +4 -7
- package/dist/events/definitions/ui.d.ts.map +1 -1
- package/dist/events/definitions/workspace.d.ts +5 -0
- package/dist/events/definitions/workspace.d.ts.map +1 -1
- package/dist/helpers/is-non-optional-security-requirement.d.ts +4 -0
- package/dist/helpers/is-non-optional-security-requirement.d.ts.map +1 -0
- package/dist/helpers/is-non-optional-security-requirement.js +7 -0
- package/dist/helpers/is-non-optional-security-requirement.js.map +7 -0
- package/dist/helpers/merge-object.d.ts +6 -1
- package/dist/helpers/merge-object.d.ts.map +1 -1
- package/dist/helpers/merge-object.js +4 -3
- package/dist/helpers/merge-object.js.map +2 -2
- package/dist/mutators/auth.d.ts.map +1 -1
- package/dist/mutators/auth.js +2 -1
- package/dist/mutators/auth.js.map +2 -2
- package/dist/mutators/index.d.ts +1 -2
- package/dist/mutators/index.d.ts.map +1 -1
- package/dist/mutators/index.js +2 -1
- package/dist/mutators/index.js.map +2 -2
- package/dist/mutators/operation.d.ts +15 -0
- package/dist/mutators/operation.d.ts.map +1 -1
- package/dist/mutators/operation.js +63 -0
- package/dist/mutators/operation.js.map +2 -2
- package/dist/mutators/tabs.js +5 -5
- package/dist/mutators/tabs.js.map +2 -2
- package/dist/mutators/workspace.d.ts +8 -0
- package/dist/mutators/workspace.d.ts.map +1 -1
- package/dist/mutators/workspace.js +7 -0
- package/dist/mutators/workspace.js.map +2 -2
- package/dist/plugins/client/index.d.ts +0 -1
- package/dist/plugins/client/index.d.ts.map +1 -1
- package/dist/plugins/client/index.js.map +2 -2
- package/dist/schemas/extensions/schema/x-examples.d.ts +10 -0
- package/dist/schemas/extensions/schema/x-examples.d.ts.map +1 -0
- package/dist/schemas/extensions/schema/x-examples.js +8 -0
- package/dist/schemas/extensions/schema/x-examples.js.map +7 -0
- package/dist/schemas/inmemory-workspace.d.ts +28 -4
- package/dist/schemas/inmemory-workspace.d.ts.map +1 -1
- package/dist/schemas/reference-config/index.d.ts +16 -4
- package/dist/schemas/reference-config/index.d.ts.map +1 -1
- package/dist/schemas/reference-config/index.js.map +2 -2
- package/dist/schemas/reference-config/settings.d.ts +13 -1
- package/dist/schemas/reference-config/settings.d.ts.map +1 -1
- package/dist/schemas/v3.1/strict/openapi-document.d.ts +442 -34
- package/dist/schemas/v3.1/strict/openapi-document.d.ts.map +1 -1
- package/dist/schemas/v3.1/strict/schema.d.ts +16 -1
- package/dist/schemas/v3.1/strict/schema.d.ts.map +1 -1
- package/dist/schemas/v3.1/strict/schema.js +2 -0
- package/dist/schemas/v3.1/strict/schema.js.map +2 -2
- package/dist/schemas/v3.1/strict/security-requirement.d.ts +4 -4
- package/dist/schemas/v3.1/strict/security-requirement.d.ts.map +1 -1
- package/dist/schemas/v3.1/strict/security-requirement.js +6 -4
- package/dist/schemas/v3.1/strict/security-requirement.js.map +2 -2
- package/dist/schemas/workspace-specification/config.d.ts +14 -2
- package/dist/schemas/workspace-specification/config.d.ts.map +1 -1
- package/dist/schemas/workspace-specification/index.d.ts +15 -3
- package/dist/schemas/workspace-specification/index.d.ts.map +1 -1
- package/dist/schemas/workspace.d.ts +93 -9
- package/dist/schemas/workspace.d.ts.map +1 -1
- package/package.json +8 -8
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { SecurityRequirementObject } from '../schemas/v3.1/strict/openapi-document.js';
|
|
2
|
+
/** Type guard to determine if a security requirement is non-optional */
|
|
3
|
+
export declare const isNonOptionalSecurityRequirement: (securityRequirement: SecurityRequirementObject | undefined) => securityRequirement is Record<string, string[]>;
|
|
4
|
+
//# sourceMappingURL=is-non-optional-security-requirement.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"is-non-optional-security-requirement.d.ts","sourceRoot":"","sources":["../../src/helpers/is-non-optional-security-requirement.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,wCAAwC,CAAA;AAEvF,wEAAwE;AACxE,eAAO,MAAM,gCAAgC,GAC3C,qBAAqB,yBAAyB,GAAG,SAAS,KACzD,mBAAmB,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,CAEhD,CAAA"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
const isNonOptionalSecurityRequirement = (securityRequirement) => {
|
|
2
|
+
return securityRequirement !== void 0 && Object.keys(securityRequirement).length > 0;
|
|
3
|
+
};
|
|
4
|
+
export {
|
|
5
|
+
isNonOptionalSecurityRequirement
|
|
6
|
+
};
|
|
7
|
+
//# sourceMappingURL=is-non-optional-security-requirement.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/helpers/is-non-optional-security-requirement.ts"],
|
|
4
|
+
"sourcesContent": ["import type { SecurityRequirementObject } from '@/schemas/v3.1/strict/openapi-document'\n\n/** Type guard to determine if a security requirement is non-optional */\nexport const isNonOptionalSecurityRequirement = (\n securityRequirement: SecurityRequirementObject | undefined,\n): securityRequirement is Record<string, string[]> => {\n return securityRequirement !== undefined && Object.keys(securityRequirement).length > 0\n}\n"],
|
|
5
|
+
"mappings": "AAGO,MAAM,mCAAmC,CAC9C,wBACoD;AACpD,SAAO,wBAAwB,UAAa,OAAO,KAAK,mBAAmB,EAAE,SAAS;AACxF;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -27,5 +27,10 @@
|
|
|
27
27
|
* const target = { age: 30 }
|
|
28
28
|
* mergeObjects(target, obj) // Safely merges without infinite recursion
|
|
29
29
|
*/
|
|
30
|
-
export declare const mergeObjects: <R>(a: Record<string, unknown>, b: Record<string, unknown>,
|
|
30
|
+
export declare const mergeObjects: <R>(a: Record<string, unknown>, b: Record<string, unknown>,
|
|
31
|
+
/**
|
|
32
|
+
* By default we overwrite array indexes, our store is built on this assumption when coercing the document
|
|
33
|
+
* Alternatively we may want to prevent this behaviour when merging with defaults and replace the whole array instead
|
|
34
|
+
*/
|
|
35
|
+
replaceArrays?: boolean, cache?: Set<unknown>) => R;
|
|
31
36
|
//# sourceMappingURL=merge-object.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"merge-object.d.ts","sourceRoot":"","sources":["../../src/helpers/merge-object.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,eAAO,MAAM,YAAY,GAAI,CAAC,EAC5B,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC1B,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,
|
|
1
|
+
{"version":3,"file":"merge-object.d.ts","sourceRoot":"","sources":["../../src/helpers/merge-object.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,eAAO,MAAM,YAAY,GAAI,CAAC,EAC5B,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC1B,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;AAC1B;;;GAGG;AACH,uBAAqB,EACrB,QAAO,GAAG,CAAC,OAAO,CAAa,KAC9B,CA4CF,CAAA"}
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { getRaw } from "@scalar/json-magic/magic-proxy";
|
|
2
|
-
const mergeObjects = (a, b, cache = /* @__PURE__ */ new Set()) => {
|
|
2
|
+
const mergeObjects = (a, b, replaceArrays = false, cache = /* @__PURE__ */ new Set()) => {
|
|
3
3
|
for (const key in b) {
|
|
4
4
|
if (!(key in a)) {
|
|
5
5
|
a[key] = b[key];
|
|
6
6
|
} else {
|
|
7
7
|
const aValue = a[key];
|
|
8
8
|
const bValue = b[key];
|
|
9
|
-
|
|
9
|
+
const shouldReplaceArrays = replaceArrays && (Array.isArray(aValue) || Array.isArray(bValue));
|
|
10
|
+
if (typeof aValue === "object" && aValue !== null && typeof bValue === "object" && bValue !== null && !shouldReplaceArrays) {
|
|
10
11
|
const rawA = getRaw(aValue);
|
|
11
12
|
const rawB = getRaw(bValue);
|
|
12
13
|
if (cache.has(rawA) || cache.has(rawB)) {
|
|
@@ -14,7 +15,7 @@ const mergeObjects = (a, b, cache = /* @__PURE__ */ new Set()) => {
|
|
|
14
15
|
}
|
|
15
16
|
cache.add(rawA);
|
|
16
17
|
cache.add(rawB);
|
|
17
|
-
mergeObjects(aValue, bValue, cache);
|
|
18
|
+
mergeObjects(aValue, bValue, replaceArrays, cache);
|
|
18
19
|
} else {
|
|
19
20
|
try {
|
|
20
21
|
a[key] = bValue;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/helpers/merge-object.ts"],
|
|
4
|
-
"sourcesContent": ["import { getRaw } from '@scalar/json-magic/magic-proxy'\n\nimport type { UnknownObject } from '@/helpers/general'\n\n/**\n * Deep merges two objects, combining their properties recursively.\n * Handles circular references by tracking visited objects to prevent infinite recursion.\n *\n * \u26A0\uFE0F Note: This operation assumes there are no key collisions between the objects.\n * Use isKeyCollisions() to check for collisions before merging.\n *\n * @param a - Target object to merge into\n * @param b - Source object to merge from\n * @param cache - Set of visited objects to prevent circular reference issues\n * @returns The merged object (mutates and returns a)\n *\n * @example\n * // Simple merge\n * const a = { name: 'John' }\n * const b = { age: 30 }\n * mergeObjects(a, b) // { name: 'John', age: 30 }\n *\n * // Nested merge\n * const a = { user: { name: 'John' } }\n * const b = { user: { age: 30 } }\n * mergeObjects(a, b) // { user: { name: 'John', age: 30 } }\n *\n * // Circular reference safe\n * const obj = { name: 'John' }\n * obj.self = obj\n * const target = { age: 30 }\n * mergeObjects(target, obj) // Safely merges without infinite recursion\n */\nexport const mergeObjects = <R>(\n a: Record<string, unknown>,\n b: Record<string, unknown>,\n cache: Set<unknown> = new Set(),\n): R => {\n for (const key in b) {\n if (!(key in a)) {\n a[key] = b[key]\n } else {\n const aValue = a[key]\n const bValue = b[key]\n\n if (typeof aValue === 'object'
|
|
5
|
-
"mappings": "AAAA,SAAS,cAAc;AAiChB,MAAM,eAAe,CAC1B,GACA,
|
|
4
|
+
"sourcesContent": ["import { getRaw } from '@scalar/json-magic/magic-proxy'\n\nimport type { UnknownObject } from '@/helpers/general'\n\n/**\n * Deep merges two objects, combining their properties recursively.\n * Handles circular references by tracking visited objects to prevent infinite recursion.\n *\n * \u26A0\uFE0F Note: This operation assumes there are no key collisions between the objects.\n * Use isKeyCollisions() to check for collisions before merging.\n *\n * @param a - Target object to merge into\n * @param b - Source object to merge from\n * @param cache - Set of visited objects to prevent circular reference issues\n * @returns The merged object (mutates and returns a)\n *\n * @example\n * // Simple merge\n * const a = { name: 'John' }\n * const b = { age: 30 }\n * mergeObjects(a, b) // { name: 'John', age: 30 }\n *\n * // Nested merge\n * const a = { user: { name: 'John' } }\n * const b = { user: { age: 30 } }\n * mergeObjects(a, b) // { user: { name: 'John', age: 30 } }\n *\n * // Circular reference safe\n * const obj = { name: 'John' }\n * obj.self = obj\n * const target = { age: 30 }\n * mergeObjects(target, obj) // Safely merges without infinite recursion\n */\nexport const mergeObjects = <R>(\n a: Record<string, unknown>,\n b: Record<string, unknown>,\n /**\n * By default we overwrite array indexes, our store is built on this assumption when coercing the document\n * Alternatively we may want to prevent this behaviour when merging with defaults and replace the whole array instead\n */\n replaceArrays = false,\n cache: Set<unknown> = new Set(),\n): R => {\n for (const key in b) {\n if (!(key in a)) {\n a[key] = b[key]\n } else {\n const aValue = a[key]\n const bValue = b[key]\n\n /** Replace whole array instead of replacing each index */\n const shouldReplaceArrays = replaceArrays && (Array.isArray(aValue) || Array.isArray(bValue))\n\n if (\n typeof aValue === 'object' &&\n aValue !== null &&\n typeof bValue === 'object' &&\n bValue !== null &&\n !shouldReplaceArrays\n ) {\n const rawA = getRaw(aValue as UnknownObject)\n const rawB = getRaw(bValue as UnknownObject)\n\n // Check for circular references before recursive merge\n if (cache.has(rawA) || cache.has(rawB)) {\n // Skip merging this branch to prevent infinite recursion\n continue\n }\n\n // Add objects to cache before recursive call\n cache.add(rawA)\n cache.add(rawB)\n\n mergeObjects(aValue as Record<string, unknown>, bValue as Record<string, unknown>, replaceArrays, cache)\n } else {\n try {\n a[key] = bValue // Overwrite with b's value if not an object\n } catch (error) {\n console.warn(`Issue setting ${key} on object`)\n console.warn(error)\n }\n }\n }\n }\n\n return a as R\n}\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,cAAc;AAiChB,MAAM,eAAe,CAC1B,GACA,GAKA,gBAAgB,OAChB,QAAsB,oBAAI,IAAI,MACxB;AACN,aAAW,OAAO,GAAG;AACnB,QAAI,EAAE,OAAO,IAAI;AACf,QAAE,GAAG,IAAI,EAAE,GAAG;AAAA,IAChB,OAAO;AACL,YAAM,SAAS,EAAE,GAAG;AACpB,YAAM,SAAS,EAAE,GAAG;AAGpB,YAAM,sBAAsB,kBAAkB,MAAM,QAAQ,MAAM,KAAK,MAAM,QAAQ,MAAM;AAE3F,UACE,OAAO,WAAW,YAClB,WAAW,QACX,OAAO,WAAW,YAClB,WAAW,QACX,CAAC,qBACD;AACA,cAAM,OAAO,OAAO,MAAuB;AAC3C,cAAM,OAAO,OAAO,MAAuB;AAG3C,YAAI,MAAM,IAAI,IAAI,KAAK,MAAM,IAAI,IAAI,GAAG;AAEtC;AAAA,QACF;AAGA,cAAM,IAAI,IAAI;AACd,cAAM,IAAI,IAAI;AAEd,qBAAa,QAAmC,QAAmC,eAAe,KAAK;AAAA,MACzG,OAAO;AACL,YAAI;AACF,YAAE,GAAG,IAAI;AAAA,QACX,SAAS,OAAO;AACd,kBAAQ,KAAK,iBAAiB,GAAG,YAAY;AAC7C,kBAAQ,KAAK,KAAK;AAAA,QACpB;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/mutators/auth.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mCAAmC,CAAA;AAEnE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAA;
|
|
1
|
+
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/mutators/auth.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mCAAmC,CAAA;AAEnE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAA;AAK3D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAA;AAGlD;;;;;;GAMG;AACH,MAAM,MAAM,QAAQ,GAChB;IACE,IAAI,EAAE,UAAU,CAAA;CACjB,GACD;IACE,IAAI,EAAE,WAAW,CAAA;IACjB,IAAI,EAAE,MAAM,CAAA;IACZ,MAAM,EAAE,UAAU,CAAA;CACnB,CAAA;AAEL;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,eAAO,MAAM,6BAA6B,GACxC,UAAU,iBAAiB,GAAG,IAAI,EAClC,4CAA4C,UAAU,CAAC,uCAAuC,CAAC,kBA+EhG,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,eAAO,MAAM,oBAAoB,GAC/B,UAAU,iBAAiB,GAAG,IAAI,EAClC,mBAAmB,UAAU,CAAC,6BAA6B,CAAC;;;;;cAc7D,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,eAAO,MAAM,qBAAqB,GAChC,UAAU,iBAAiB,GAAG,IAAI,EAClC,iBAAiB,UAAU,CAAC,0BAA0B,CAAC,SA+BxD,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,eAAO,MAAM,oBAAoB,GAC/B,UAAU,iBAAiB,GAAG,IAAI,EAClC,4BAA4B,UAAU,CAAC,6BAA6B,CAAC,SAsCtE,CAAA;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,oBAAoB,GAC/B,UAAU,iBAAiB,GAAG,IAAI,EAClC,WAAW,UAAU,CAAC,6BAA6B,CAAC,SA6DrD,CAAA"}
|
package/dist/mutators/auth.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { generateUniqueValue } from "../helpers/generate-unique-value.js";
|
|
2
2
|
import { getResolvedRef } from "../helpers/get-resolved-ref.js";
|
|
3
|
+
import { isNonOptionalSecurityRequirement } from "../helpers/is-non-optional-security-requirement.js";
|
|
3
4
|
import { mergeObjects } from "../helpers/merge-object.js";
|
|
4
5
|
const updateSelectedSecuritySchemes = async (document, { selectedRequirements, newSchemes, meta }) => {
|
|
5
6
|
if (!document) {
|
|
@@ -106,7 +107,7 @@ const updateSelectedScopes = (document, { id, name, scopes, meta }) => {
|
|
|
106
107
|
return;
|
|
107
108
|
}
|
|
108
109
|
const scheme = selectedSchemes.find((scheme2) => JSON.stringify(Object.keys(scheme2)) === JSON.stringify(id));
|
|
109
|
-
if (!scheme) {
|
|
110
|
+
if (!isNonOptionalSecurityRequirement(scheme)) {
|
|
110
111
|
return;
|
|
111
112
|
}
|
|
112
113
|
scheme[name] = scopes;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/mutators/auth.ts"],
|
|
4
|
-
"sourcesContent": ["import type { HttpMethod } from '@scalar/helpers/http/http-methods'\n\nimport type { AuthEvents } from '@/events/definitions/auth'\nimport { generateUniqueValue } from '@/helpers/generate-unique-value'\nimport { getResolvedRef } from '@/helpers/get-resolved-ref'\nimport { mergeObjects } from '@/helpers/merge-object'\nimport type { WorkspaceDocument } from '@/schemas'\nimport type { SecurityRequirementObject } from '@/schemas/v3.1/strict/security-requirement'\n\n/**\n * AuthMeta defines the meta information needed to specify whether the authentication operation\n * is being performed at the document level (entire API), or for a specific operation (specific path and method).\n *\n * - If type is 'document', the operation applies to the whole OpenAPI document.\n * - If type is 'operation', it targets a specific operation, identified by its path and method.\n */\nexport type AuthMeta =\n | {\n type: 'document'\n }\n | {\n type: 'operation'\n path: string\n method: HttpMethod\n }\n\n/**\n * Updates the selected security schemes for either the entire document or a specific operation.\n * - Adds newly created security schemes (if any) to the workspace document's components.\n * - Ensures that each new scheme name is unique within the document by using `generateUniqueValue`.\n * - Updates the `x-scalar-selected-security` property on the target (document or operation) to reflect the new set of selected security schemes.\n * - Corrects and maintains the selected index so it points to a valid security scheme.\n *\n * @param document - The workspace OpenAPI document to mutate (can be null, in which case nothing happens)\n * @param selectedSecuritySchemes - The current list of selected security scheme objects\n * @param create - Array of new schemes to create, each with a name and a scheme definition\n * @param meta - Location to update: whole document or a specific operation (`{ type: 'document' }` or `{ type: 'operation', path, method }`)\n *\n * Example usage:\n * ```\n * updateSelectedSecuritySchemes({\n * document,\n * selectedSecuritySchemes: [{ bearerAuth: [] }],\n * create: [\n * { name: 'ApiKeyAuth', scheme: { type: 'apiKey', in: 'header', name: 'X-API-Key' } }\n * ],\n * meta: { type: 'document' }\n * })\n * ```\n */\nexport const updateSelectedSecuritySchemes = async (\n document: WorkspaceDocument | null,\n { selectedRequirements, newSchemes, meta }: AuthEvents['auth:update:selected-security-schemes'],\n) => {\n if (!document) {\n return\n }\n\n // Helper to get the target (whole document or a specific operation)\n const getTarget = () => {\n if (meta.type === 'document') {\n return document\n }\n\n return getResolvedRef(document.paths?.[meta.path]?.[meta.method])\n }\n\n const createdSecurityRequirements = await Promise.all(\n newSchemes.map(async (newScheme) => {\n const uniqueSchemeName = await generateUniqueValue({\n defaultValue: newScheme.name,\n validation: (value) => !document.components?.securitySchemes?.[value],\n maxRetries: 100,\n })\n\n if (!uniqueSchemeName) {\n return\n }\n\n // Ensure components and securitySchemes exist\n if (!document.components) {\n document.components = {}\n }\n if (!document.components.securitySchemes) {\n document.components.securitySchemes = {}\n }\n\n // Add the new security scheme definition\n document.components.securitySchemes[uniqueSchemeName] = newScheme.scheme\n\n // Return an OpenAPI Security Requirement Object for this new scheme (empty scope array)\n return {\n [uniqueSchemeName]: [],\n }\n }),\n )\n\n // Create any new security schemes required, ensuring unique names for the components\n const createdSchemes = createdSecurityRequirements.filter(Boolean) as SecurityRequirementObject[]\n\n const target = getTarget()\n\n const newSelectedSecuritySchemes = [...selectedRequirements, ...createdSchemes]\n\n // If the target (document/operation) doesn't exist, do nothing\n if (!target) {\n return\n }\n\n // Ensure the x-scalar-selected-security structure exists on the target\n if (!target['x-scalar-selected-security']) {\n target['x-scalar-selected-security'] = {\n selectedIndex: -1,\n selectedSchemes: [],\n }\n }\n\n const selectedIndex = target['x-scalar-selected-security'].selectedIndex\n\n // Update the schemes array\n target['x-scalar-selected-security'].selectedSchemes = newSelectedSecuritySchemes\n\n // Adjust selected index if there are schemes and the index is unset/invalid\n if (newSelectedSecuritySchemes.length > 0 && selectedIndex < 0) {\n target['x-scalar-selected-security'].selectedIndex = 0\n }\n\n // If the selected index is now out of bounds, select the last available\n if (selectedIndex >= newSelectedSecuritySchemes.length) {\n target['x-scalar-selected-security'].selectedIndex = newSelectedSecuritySchemes.length - 1\n }\n}\n\n/**\n * Updates a security scheme in the OpenAPI document's components object.\n * Handles updates for HTTP, API Key, and OAuth2 types, saving secret information and configuration for UI-auth flows.\n *\n * @param document - The OpenAPI workspace document (can be null)\n * @param data - The update information, including type and payload\n * @param name - The name of the security scheme in document.components.securitySchemes\n *\n * Example usage:\n *\n * updateSecurityScheme({\n * document,\n * data: {\n * type: 'http',\n * payload: {\n * username: 'user123',\n * password: 'pw123',\n * token: 'tokenval'\n * }\n * },\n * name: 'MyHttpAuth',\n * })\n */\nexport const updateSecurityScheme = (\n document: WorkspaceDocument | null,\n { payload, name }: AuthEvents['auth:update:security-scheme'],\n) => {\n const target = getResolvedRef(document?.components?.securitySchemes?.[name])\n if (!target) {\n console.error(`Security scheme ${name} not found`)\n return\n }\n\n // Handle HTTP (basic, bearer, etc.)\n if (target.type === payload.type) {\n mergeObjects(target, payload)\n }\n\n return target\n}\n\n/**\n * Sets the selected authentication tab (scheme) index for the given OpenAPI document or operation.\n * - When on the document level, updates the 'selectedIndex' on the document's x-scalar-selected-security extension.\n * - When on an operation (endpoint) level, updates the 'selectedIndex' for that operation's x-scalar-selected-security.\n *\n * Also initializes the x-scalar-selected-security extension if it does not exist.\n *\n * @param document The OpenAPI document object (may be null)\n * @param index The index to set as selected\n * @param meta Context where the selection applies ('document' or specific operation)\n *\n * @example\n * // Document-level tab selection\n * updateSelectedAuthTab({\n * document,\n * index: 1,\n * meta: { type: 'document' }\n * });\n *\n * // Operation-level tab selection (e.g., GET /pets)\n * updateSelectedAuthTab({\n * document,\n * index: 0,\n * meta: { type: 'operation', path: '/pets', method: 'get' }\n * });\n */\nexport const updateSelectedAuthTab = (\n document: WorkspaceDocument | null,\n { index, meta }: AuthEvents['auth:update:active-index'],\n) => {\n if (!document) {\n return\n }\n\n // Determine the target object for setting the auth tab index:\n // - Document/root level\n // - Operation/endpoint level (if meta specifies operation)\n const getTarget = () => {\n if (meta.type === 'document') {\n return document\n }\n return getResolvedRef(document.paths?.[meta.path]?.[meta.method])\n }\n\n const target = getTarget()\n if (!target) {\n return\n }\n\n // Ensure the 'x-scalar-selected-security' extension exists\n if (!target['x-scalar-selected-security']) {\n target['x-scalar-selected-security'] = {\n selectedIndex: 0,\n selectedSchemes: [],\n }\n }\n\n // Set the selected auth tab index\n target['x-scalar-selected-security'].selectedIndex = index\n}\n\n/**\n * Updates the scopes for a specific security requirement in the selected security schemes of\n * a document or operation.\n *\n * @param document - The OpenAPI WorkspaceDocument to update.\n * @param id - An array of scheme names that uniquely identifies the target security requirement.\n * For example: ['OAuth', 'ApiKeyAuth']\n * @param name - The security scheme name to update scopes for (e.g., 'OAuth').\n * @param scopes - The new list of scopes to set. For example: ['read:pets', 'write:pets']\n * @param meta - The context specifying whether the update is at the document-level or operation-level.\n *\n * Example usage:\n * ```ts\n * // Suppose your document (or operation) x-scalar-selected-security looks like:\n * // \"x-scalar-selected-security\": {\n * // selectedIndex: 0,\n * // selectedSchemes: [\n * // { \"OAuth\": [\"read:pets\"] },\n * // { \"ApiKeyAuth\": [] }\n * // ]\n * // }\n *\n * updateSelectedScopes({\n * document,\n * id: [\"OAuth\"], // identifies the scheme object: { \"OAuth\": [...] }\n * name: \"OAuth\", // scheme name to update within this security requirement\n * scopes: [\"write:pets\"], // new scopes array\n * meta: { type: \"document\" }\n * })\n * // After, the first scheme becomes: { \"OAuth\": [\"write:pets\"] }\n * ```\n */\nexport const updateSelectedScopes = (\n document: WorkspaceDocument | null,\n { id, name, scopes, meta }: AuthEvents['auth:update:selected-scopes'],\n) => {\n if (!document) {\n return\n }\n\n // Determine the target object (document or the operation)\n const getTarget = () => {\n if (meta.type === 'document') {\n return document\n }\n return getResolvedRef(document.paths?.[meta.path]?.[meta.method])\n }\n\n const target = getTarget()\n\n if (!target) {\n return\n }\n\n // Array of security requirement objects under x-scalar-selected-security\n const selectedSchemes = target['x-scalar-selected-security']?.selectedSchemes\n\n if (!selectedSchemes) {\n return\n }\n\n // Find the security requirement that matches the given id (scheme key names)\n // For example: if id = [\"OAuth\"], matches { OAuth: [...] }\n const scheme = selectedSchemes.find((scheme) => JSON.stringify(Object.keys(scheme)) === JSON.stringify(id))\n\n if (!scheme) {\n return\n }\n\n // Set the scopes array for the named security scheme within the found security requirement\n scheme[name] = scopes\n}\n\n/**\n * Deletes one or more security schemes from an OpenAPI WorkspaceDocument,\n * and removes all references to those schemes from selected security, document-level security,\n * and operation-level security/selected security (e.g., on paths).\n *\n * Example usage:\n *\n * ```ts\n * deleteSecurityScheme({\n * document, // The OpenAPI document to update\n * names: ['ApiKeyAuth', 'BearerAuth'], // The names of security schemes you want to delete\n * });\n * ```\n *\n * After running this function:\n * - The named security schemes are removed from the components.securitySchemes section.\n * - All document-level and operation-level security entries referencing those schemes are removed.\n * - Any extended x-scalar-selected-security references to those schemes are also removed.\n */\nexport const deleteSecurityScheme = (\n document: WorkspaceDocument | null,\n { names }: AuthEvents['auth:delete:security-scheme'],\n) => {\n if (!document) {\n // Early exit if there is no document to modify\n return\n }\n\n // Get the mutable reference to securitySchemes in components (may be a proxy/resolved reference)\n const target = getResolvedRef(document.components?.securitySchemes)\n\n if (!target) {\n // If there are no security schemes to delete from, return early\n return\n }\n\n // Remove each named security scheme from the components.securitySchemes object\n names.forEach((name) => {\n delete target[name]\n })\n\n // Function to remove any security requirement objects that reference given scheme names.\n const filterSecuritySchemes = (schemes: SecurityRequirementObject[]) => {\n // Remove schemes whose key is included in the `names` to be deleted.\n return schemes.filter((scheme) => !names.some((name) => Object.keys(scheme).includes(name)))\n }\n\n // -- Remove from document-level `x-scalar-selected-security` extension, if present\n if (document['x-scalar-selected-security']) {\n const selectedSecurity = document['x-scalar-selected-security']\n selectedSecurity.selectedSchemes = filterSecuritySchemes(selectedSecurity.selectedSchemes)\n }\n\n // -- Remove from document-level `security` property, if present\n if (document['security']) {\n document['security'] = filterSecuritySchemes(document['security'])\n }\n\n // -- For each path and operation, remove deleted security schemes from operation-level security and custom extension\n Object.values(document.paths ?? {}).forEach((path) => {\n Object.values(path).forEach((operation) => {\n if (typeof operation !== 'object') {\n // Ignore operations that are not objects (could be undefined)\n return\n }\n\n // Get mutable reference for the operation (could resolve $ref proxies)\n const resolvedOperation = getResolvedRef(operation)\n\n // Remove from operation-level security array\n if ('security' in resolvedOperation && resolvedOperation['security']) {\n resolvedOperation['security'] = filterSecuritySchemes(resolvedOperation['security'])\n }\n\n // Remove from operation-level x-scalar-selected-security array\n if ('x-scalar-selected-security' in resolvedOperation && resolvedOperation['x-scalar-selected-security']) {\n resolvedOperation['x-scalar-selected-security'].selectedSchemes = filterSecuritySchemes(\n resolvedOperation['x-scalar-selected-security'].selectedSchemes,\n )\n }\n })\n })\n}\n"],
|
|
5
|
-
"mappings": "AAGA,SAAS,2BAA2B;AACpC,SAAS,sBAAsB;AAC/B,SAAS,oBAAoB;AA6CtB,MAAM,gCAAgC,OAC3C,UACA,EAAE,sBAAsB,YAAY,KAAK,MACtC;AACH,MAAI,CAAC,UAAU;AACb;AAAA,EACF;AAGA,QAAM,YAAY,MAAM;AACtB,QAAI,KAAK,SAAS,YAAY;AAC5B,aAAO;AAAA,IACT;AAEA,WAAO,eAAe,SAAS,QAAQ,KAAK,IAAI,IAAI,KAAK,MAAM,CAAC;AAAA,EAClE;AAEA,QAAM,8BAA8B,MAAM,QAAQ;AAAA,IAChD,WAAW,IAAI,OAAO,cAAc;AAClC,YAAM,mBAAmB,MAAM,oBAAoB;AAAA,QACjD,cAAc,UAAU;AAAA,QACxB,YAAY,CAAC,UAAU,CAAC,SAAS,YAAY,kBAAkB,KAAK;AAAA,QACpE,YAAY;AAAA,MACd,CAAC;AAED,UAAI,CAAC,kBAAkB;AACrB;AAAA,MACF;AAGA,UAAI,CAAC,SAAS,YAAY;AACxB,iBAAS,aAAa,CAAC;AAAA,MACzB;AACA,UAAI,CAAC,SAAS,WAAW,iBAAiB;AACxC,iBAAS,WAAW,kBAAkB,CAAC;AAAA,MACzC;AAGA,eAAS,WAAW,gBAAgB,gBAAgB,IAAI,UAAU;AAGlE,aAAO;AAAA,QACL,CAAC,gBAAgB,GAAG,CAAC;AAAA,MACvB;AAAA,IACF,CAAC;AAAA,EACH;AAGA,QAAM,iBAAiB,4BAA4B,OAAO,OAAO;AAEjE,QAAM,SAAS,UAAU;AAEzB,QAAM,6BAA6B,CAAC,GAAG,sBAAsB,GAAG,cA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O,MAAM,uBAAuB,CAClC,UACA,EAAE,IAAI,MAAM,QAAQ,KAAK,MACtB;AACH,MAAI,CAAC,UAAU;AACb;AAAA,EACF;AAGA,QAAM,YAAY,MAAM;AACtB,QAAI,KAAK,SAAS,YAAY;AAC5B,aAAO;AAAA,IACT;AACA,WAAO,eAAe,SAAS,QAAQ,KAAK,IAAI,IAAI,KAAK,MAAM,CAAC;AAAA,EAClE;AAEA,QAAM,SAAS,UAAU;AAEzB,MAAI,CAAC,QAAQ;AACX;AAAA,EACF;AAGA,QAAM,kBAAkB,OAAO,4BAA4B,GAAG;AAE9D,MAAI,CAAC,iBAAiB;AACpB;AAAA,EACF;AAIA,QAAM,SAAS,gBAAgB,KAAK,CAACA,YAAW,KAAK,UAAU,OAAO,KAAKA,OAAM,CAAC,MAAM,KAAK,UAAU,EAAE,CAAC;
|
|
4
|
+
"sourcesContent": ["import type { HttpMethod } from '@scalar/helpers/http/http-methods'\n\nimport type { AuthEvents } from '@/events/definitions/auth'\nimport { generateUniqueValue } from '@/helpers/generate-unique-value'\nimport { getResolvedRef } from '@/helpers/get-resolved-ref'\nimport { isNonOptionalSecurityRequirement } from '@/helpers/is-non-optional-security-requirement'\nimport { mergeObjects } from '@/helpers/merge-object'\nimport type { WorkspaceDocument } from '@/schemas'\nimport type { SecurityRequirementObject } from '@/schemas/v3.1/strict/security-requirement'\n\n/**\n * AuthMeta defines the meta information needed to specify whether the authentication operation\n * is being performed at the document level (entire API), or for a specific operation (specific path and method).\n *\n * - If type is 'document', the operation applies to the whole OpenAPI document.\n * - If type is 'operation', it targets a specific operation, identified by its path and method.\n */\nexport type AuthMeta =\n | {\n type: 'document'\n }\n | {\n type: 'operation'\n path: string\n method: HttpMethod\n }\n\n/**\n * Updates the selected security schemes for either the entire document or a specific operation.\n * - Adds newly created security schemes (if any) to the workspace document's components.\n * - Ensures that each new scheme name is unique within the document by using `generateUniqueValue`.\n * - Updates the `x-scalar-selected-security` property on the target (document or operation) to reflect the new set of selected security schemes.\n * - Corrects and maintains the selected index so it points to a valid security scheme.\n *\n * @param document - The workspace OpenAPI document to mutate (can be null, in which case nothing happens)\n * @param selectedSecuritySchemes - The current list of selected security scheme objects\n * @param create - Array of new schemes to create, each with a name and a scheme definition\n * @param meta - Location to update: whole document or a specific operation (`{ type: 'document' }` or `{ type: 'operation', path, method }`)\n *\n * Example usage:\n * ```\n * updateSelectedSecuritySchemes({\n * document,\n * selectedSecuritySchemes: [{ bearerAuth: [] }],\n * create: [\n * { name: 'ApiKeyAuth', scheme: { type: 'apiKey', in: 'header', name: 'X-API-Key' } }\n * ],\n * meta: { type: 'document' }\n * })\n * ```\n */\nexport const updateSelectedSecuritySchemes = async (\n document: WorkspaceDocument | null,\n { selectedRequirements, newSchemes, meta }: AuthEvents['auth:update:selected-security-schemes'],\n) => {\n if (!document) {\n return\n }\n\n // Helper to get the target (whole document or a specific operation)\n const getTarget = () => {\n if (meta.type === 'document') {\n return document\n }\n\n return getResolvedRef(document.paths?.[meta.path]?.[meta.method])\n }\n\n const createdSecurityRequirements = await Promise.all(\n newSchemes.map(async (newScheme) => {\n const uniqueSchemeName = await generateUniqueValue({\n defaultValue: newScheme.name,\n validation: (value) => !document.components?.securitySchemes?.[value],\n maxRetries: 100,\n })\n\n if (!uniqueSchemeName) {\n return\n }\n\n // Ensure components and securitySchemes exist\n if (!document.components) {\n document.components = {}\n }\n if (!document.components.securitySchemes) {\n document.components.securitySchemes = {}\n }\n\n // Add the new security scheme definition\n document.components.securitySchemes[uniqueSchemeName] = newScheme.scheme\n\n // Return an OpenAPI Security Requirement Object for this new scheme (empty scope array)\n return {\n [uniqueSchemeName]: [],\n }\n }),\n )\n\n // Create any new security schemes required, ensuring unique names for the components\n const createdSchemes = createdSecurityRequirements.filter(Boolean) as SecurityRequirementObject[]\n\n const target = getTarget()\n\n const newSelectedSecuritySchemes = [...selectedRequirements, ...createdSchemes]\n\n // If the target (document/operation) doesn't exist, do nothing\n if (!target) {\n return\n }\n\n // Ensure the x-scalar-selected-security structure exists on the target\n if (!target['x-scalar-selected-security']) {\n target['x-scalar-selected-security'] = {\n selectedIndex: -1,\n selectedSchemes: [],\n }\n }\n\n const selectedIndex = target['x-scalar-selected-security'].selectedIndex\n\n // Update the schemes array\n target['x-scalar-selected-security'].selectedSchemes = newSelectedSecuritySchemes\n\n // Adjust selected index if there are schemes and the index is unset/invalid\n if (newSelectedSecuritySchemes.length > 0 && selectedIndex < 0) {\n target['x-scalar-selected-security'].selectedIndex = 0\n }\n\n // If the selected index is now out of bounds, select the last available\n if (selectedIndex >= newSelectedSecuritySchemes.length) {\n target['x-scalar-selected-security'].selectedIndex = newSelectedSecuritySchemes.length - 1\n }\n}\n\n/**\n * Updates a security scheme in the OpenAPI document's components object.\n * Handles updates for HTTP, API Key, and OAuth2 types, saving secret information and configuration for UI-auth flows.\n *\n * @param document - The OpenAPI workspace document (can be null)\n * @param data - The update information, including type and payload\n * @param name - The name of the security scheme in document.components.securitySchemes\n *\n * Example usage:\n *\n * updateSecurityScheme({\n * document,\n * data: {\n * type: 'http',\n * payload: {\n * username: 'user123',\n * password: 'pw123',\n * token: 'tokenval'\n * }\n * },\n * name: 'MyHttpAuth',\n * })\n */\nexport const updateSecurityScheme = (\n document: WorkspaceDocument | null,\n { payload, name }: AuthEvents['auth:update:security-scheme'],\n) => {\n const target = getResolvedRef(document?.components?.securitySchemes?.[name])\n if (!target) {\n console.error(`Security scheme ${name} not found`)\n return\n }\n\n // Handle HTTP (basic, bearer, etc.)\n if (target.type === payload.type) {\n mergeObjects(target, payload)\n }\n\n return target\n}\n\n/**\n * Sets the selected authentication tab (scheme) index for the given OpenAPI document or operation.\n * - When on the document level, updates the 'selectedIndex' on the document's x-scalar-selected-security extension.\n * - When on an operation (endpoint) level, updates the 'selectedIndex' for that operation's x-scalar-selected-security.\n *\n * Also initializes the x-scalar-selected-security extension if it does not exist.\n *\n * @param document The OpenAPI document object (may be null)\n * @param index The index to set as selected\n * @param meta Context where the selection applies ('document' or specific operation)\n *\n * @example\n * // Document-level tab selection\n * updateSelectedAuthTab({\n * document,\n * index: 1,\n * meta: { type: 'document' }\n * });\n *\n * // Operation-level tab selection (e.g., GET /pets)\n * updateSelectedAuthTab({\n * document,\n * index: 0,\n * meta: { type: 'operation', path: '/pets', method: 'get' }\n * });\n */\nexport const updateSelectedAuthTab = (\n document: WorkspaceDocument | null,\n { index, meta }: AuthEvents['auth:update:active-index'],\n) => {\n if (!document) {\n return\n }\n\n // Determine the target object for setting the auth tab index:\n // - Document/root level\n // - Operation/endpoint level (if meta specifies operation)\n const getTarget = () => {\n if (meta.type === 'document') {\n return document\n }\n return getResolvedRef(document.paths?.[meta.path]?.[meta.method])\n }\n\n const target = getTarget()\n if (!target) {\n return\n }\n\n // Ensure the 'x-scalar-selected-security' extension exists\n if (!target['x-scalar-selected-security']) {\n target['x-scalar-selected-security'] = {\n selectedIndex: 0,\n selectedSchemes: [],\n }\n }\n\n // Set the selected auth tab index\n target['x-scalar-selected-security'].selectedIndex = index\n}\n\n/**\n * Updates the scopes for a specific security requirement in the selected security schemes of\n * a document or operation.\n *\n * @param document - The OpenAPI WorkspaceDocument to update.\n * @param id - An array of scheme names that uniquely identifies the target security requirement.\n * For example: ['OAuth', 'ApiKeyAuth']\n * @param name - The security scheme name to update scopes for (e.g., 'OAuth').\n * @param scopes - The new list of scopes to set. For example: ['read:pets', 'write:pets']\n * @param meta - The context specifying whether the update is at the document-level or operation-level.\n *\n * Example usage:\n * ```ts\n * // Suppose your document (or operation) x-scalar-selected-security looks like:\n * // \"x-scalar-selected-security\": {\n * // selectedIndex: 0,\n * // selectedSchemes: [\n * // { \"OAuth\": [\"read:pets\"] },\n * // { \"ApiKeyAuth\": [] }\n * // ]\n * // }\n *\n * updateSelectedScopes({\n * document,\n * id: [\"OAuth\"], // identifies the scheme object: { \"OAuth\": [...] }\n * name: \"OAuth\", // scheme name to update within this security requirement\n * scopes: [\"write:pets\"], // new scopes array\n * meta: { type: \"document\" }\n * })\n * // After, the first scheme becomes: { \"OAuth\": [\"write:pets\"] }\n * ```\n */\nexport const updateSelectedScopes = (\n document: WorkspaceDocument | null,\n { id, name, scopes, meta }: AuthEvents['auth:update:selected-scopes'],\n) => {\n if (!document) {\n return\n }\n\n // Determine the target object (document or the operation)\n const getTarget = () => {\n if (meta.type === 'document') {\n return document\n }\n return getResolvedRef(document.paths?.[meta.path]?.[meta.method])\n }\n\n const target = getTarget()\n\n if (!target) {\n return\n }\n\n // Array of security requirement objects under x-scalar-selected-security\n const selectedSchemes = target['x-scalar-selected-security']?.selectedSchemes\n\n if (!selectedSchemes) {\n return\n }\n\n // Find the security requirement that matches the given id (scheme key names)\n // For example: if id = [\"OAuth\"], matches { OAuth: [...] }\n const scheme = selectedSchemes.find((scheme) => JSON.stringify(Object.keys(scheme)) === JSON.stringify(id))\n\n // If the scheme is optional, do nothing as it cannot have scopes\n if (!isNonOptionalSecurityRequirement(scheme)) {\n return\n }\n\n // Set the scopes array for the named security scheme within the found security requirement\n scheme[name] = scopes\n}\n\n/**\n * Deletes one or more security schemes from an OpenAPI WorkspaceDocument,\n * and removes all references to those schemes from selected security, document-level security,\n * and operation-level security/selected security (e.g., on paths).\n *\n * Example usage:\n *\n * ```ts\n * deleteSecurityScheme({\n * document, // The OpenAPI document to update\n * names: ['ApiKeyAuth', 'BearerAuth'], // The names of security schemes you want to delete\n * });\n * ```\n *\n * After running this function:\n * - The named security schemes are removed from the components.securitySchemes section.\n * - All document-level and operation-level security entries referencing those schemes are removed.\n * - Any extended x-scalar-selected-security references to those schemes are also removed.\n */\nexport const deleteSecurityScheme = (\n document: WorkspaceDocument | null,\n { names }: AuthEvents['auth:delete:security-scheme'],\n) => {\n if (!document) {\n // Early exit if there is no document to modify\n return\n }\n\n // Get the mutable reference to securitySchemes in components (may be a proxy/resolved reference)\n const target = getResolvedRef(document.components?.securitySchemes)\n\n if (!target) {\n // If there are no security schemes to delete from, return early\n return\n }\n\n // Remove each named security scheme from the components.securitySchemes object\n names.forEach((name) => {\n delete target[name]\n })\n\n // Function to remove any security requirement objects that reference given scheme names.\n const filterSecuritySchemes = (schemes: SecurityRequirementObject[]) => {\n // Remove schemes whose key is included in the `names` to be deleted.\n return schemes.filter((scheme) => !names.some((name) => Object.keys(scheme).includes(name)))\n }\n\n // -- Remove from document-level `x-scalar-selected-security` extension, if present\n if (document['x-scalar-selected-security']) {\n const selectedSecurity = document['x-scalar-selected-security']\n selectedSecurity.selectedSchemes = filterSecuritySchemes(selectedSecurity.selectedSchemes)\n }\n\n // -- Remove from document-level `security` property, if present\n if (document['security']) {\n document['security'] = filterSecuritySchemes(document['security'])\n }\n\n // -- For each path and operation, remove deleted security schemes from operation-level security and custom extension\n Object.values(document.paths ?? {}).forEach((path) => {\n Object.values(path).forEach((operation) => {\n if (typeof operation !== 'object') {\n // Ignore operations that are not objects (could be undefined)\n return\n }\n\n // Get mutable reference for the operation (could resolve $ref proxies)\n const resolvedOperation = getResolvedRef(operation)\n\n // Remove from operation-level security array\n if ('security' in resolvedOperation && resolvedOperation['security']) {\n resolvedOperation['security'] = filterSecuritySchemes(resolvedOperation['security'])\n }\n\n // Remove from operation-level x-scalar-selected-security array\n if ('x-scalar-selected-security' in resolvedOperation && resolvedOperation['x-scalar-selected-security']) {\n resolvedOperation['x-scalar-selected-security'].selectedSchemes = filterSecuritySchemes(\n resolvedOperation['x-scalar-selected-security'].selectedSchemes,\n )\n }\n })\n })\n}\n"],
|
|
5
|
+
"mappings": "AAGA,SAAS,2BAA2B;AACpC,SAAS,sBAAsB;AAC/B,SAAS,wCAAwC;AACjD,SAAS,oBAAoB;AA6CtB,MAAM,gCAAgC,OAC3C,UACA,EAAE,sBAAsB,YAAY,KAAK,MACtC;AACH,MAAI,CAAC,UAAU;AACb;AAAA,EACF;AAGA,QAAM,YAAY,MAAM;AACtB,QAAI,KAAK,SAAS,YAAY;AAC5B,aAAO;AAAA,IACT;AAEA,WAAO,eAAe,SAAS,QAAQ,KAAK,IAAI,IAAI,KAAK,MAAM,CAAC;AAA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|
package/dist/mutators/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/** biome-ignore-all lint/performance/noBarrelFile: Mutators entry point */
|
|
2
1
|
import type { WorkspaceStore } from '../client.js';
|
|
3
2
|
import type { ServerObject } from '../schemas/v3.1/strict/openapi-document.js';
|
|
4
3
|
/**
|
|
@@ -93,5 +92,5 @@ export { type OperationExampleMeta, type OperationMeta, addOperationParameter, a
|
|
|
93
92
|
export { addServer, deleteServer, updateSelectedServer, updateServer, updateServerVariables, } from './server.js';
|
|
94
93
|
export { addTab, closeOtherTabs, closeTab, focusLastTab, focusTab, navigateNextTab, navigatePreviousTab, updateTabs, } from './tabs.js';
|
|
95
94
|
export { createTag, deleteTag } from './tag.js';
|
|
96
|
-
export { updateActiveProxy, updateColorMode, updateTheme } from './workspace.js';
|
|
95
|
+
export { updateActiveProxy, updateColorMode, updateSelectedClient, updateTheme } from './workspace.js';
|
|
97
96
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/mutators/index.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/mutators/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAA;AAI9C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,wCAAwC,CAAA;AAE1E;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,cAAc;IAmGxD;;OAEG;;;gCAxFwB,YAAY,KAAG,OAAO;gCAatB,MAAM,KAAG,OAAO;;;;;;;IA6E3C;;;OAGG;;;;;;;;;;;;;;;;;;;;;;;gCA7FwB,YAAY,KAAG,OAAO;gCAatB,MAAM,KAAG,OAAO;;;IAmF3C;;;;OAIG;gBACS,MAAM;;;;;;;;;;;;;;;;;;;;;;gCArGS,YAAY,KAAG,OAAO;gCAatB,MAAM,KAAG,OAAO;;;EA0F9C;AAED,OAAO,EACL,KAAK,QAAQ,EACb,oBAAoB,EACpB,oBAAoB,EACpB,qBAAqB,EACrB,oBAAoB,EACpB,6BAA6B,GAC9B,MAAM,QAAQ,CAAA;AACf,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,UAAU,CAAA;AACrD,OAAO,EAAE,mBAAmB,EAAE,cAAc,EAAE,cAAc,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AACrH,OAAO,EACL,iBAAiB,EACjB,yBAAyB,GAC1B,MAAM,eAAe,CAAA;AACtB,OAAO,EACL,KAAK,oBAAoB,EACzB,KAAK,aAAa,EAClB,qBAAqB,EACrB,8BAA8B,EAC9B,eAAe,EACf,4BAA4B,EAC5B,eAAe,EACf,sBAAsB,EACtB,wBAAwB,EACxB,iCAAiC,EACjC,wBAAwB,EACxB,yBAAyB,EACzB,qCAAqC,EACrC,iCAAiC,EACjC,iCAAiC,EACjC,sBAAsB,GACvB,MAAM,aAAa,CAAA;AACpB,OAAO,EACL,SAAS,EACT,YAAY,EACZ,oBAAoB,EACpB,YAAY,EACZ,qBAAqB,GACtB,MAAM,UAAU,CAAA;AACjB,OAAO,EACL,MAAM,EACN,cAAc,EACd,QAAQ,EACR,YAAY,EACZ,QAAQ,EACR,eAAe,EACf,mBAAmB,EACnB,UAAU,GACX,MAAM,QAAQ,CAAA;AACf,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAC5C,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,oBAAoB,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA"}
|
package/dist/mutators/index.js
CHANGED
|
@@ -121,7 +121,7 @@ import {
|
|
|
121
121
|
updateTabs
|
|
122
122
|
} from "./tabs.js";
|
|
123
123
|
import { createTag, deleteTag } from "./tag.js";
|
|
124
|
-
import { updateActiveProxy, updateColorMode, updateTheme } from "./workspace.js";
|
|
124
|
+
import { updateActiveProxy, updateColorMode, updateSelectedClient, updateTheme } from "./workspace.js";
|
|
125
125
|
export {
|
|
126
126
|
addOperationParameter,
|
|
127
127
|
addOperationRequestBodyFormRow,
|
|
@@ -159,6 +159,7 @@ export {
|
|
|
159
159
|
updateOperationSummary,
|
|
160
160
|
updateSecurityScheme,
|
|
161
161
|
updateSelectedAuthTab,
|
|
162
|
+
updateSelectedClient,
|
|
162
163
|
updateSelectedScopes,
|
|
163
164
|
updateSelectedSecuritySchemes,
|
|
164
165
|
updateSelectedServer,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/mutators/index.ts"],
|
|
4
|
-
"sourcesContent": ["
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["import type { WorkspaceStore } from '@/client'\nimport { getDocument } from '@/mutators/helpers'\nimport { requestMutators } from '@/mutators/request'\nimport { securitySchemeMutators } from '@/mutators/security-schemes'\nimport type { ServerObject } from '@/schemas/v3.1/strict/openapi-document'\n\n/**\n * Generates a set of mutators for managing OpenAPI document and workspace state.\n *\n * @deprecated use the individual mutators instead, this will be removed after we move fully to the new store\n *\n * @param store - The workspace store containing all documents and workspace-level data\n * @returns An object with mutators for the workspace, the active document, and any named document\n */\nexport function generateClientMutators(store: WorkspaceStore) {\n /**\n * Provides mutator functions for managing an array of OpenAPI ServerObject entries.\n *\n * @param target - The array of ServerObject to mutate. If not provided, mutators will be no-ops.\n * @returns An object with addServer and deleteServer methods.\n */\n const serverMutators = (target?: ServerObject[]) => {\n /**\n * Adds a new ServerObject to the target array.\n * @param server - The ServerObject to add.\n * @returns true if the server was added, false if target is undefined.\n */\n const addServer = (server: ServerObject): boolean => {\n if (!target) {\n return false\n }\n target.push(server)\n return true\n }\n\n /**\n * Deletes a ServerObject at the specified index from the target array.\n * @param index - The index of the server to delete.\n * @returns true if the server was deleted, false if target is undefined.\n */\n const deleteServer = (url: string): boolean => {\n if (!target) {\n return false\n }\n const newTarget = [...target.filter((it) => it.url !== url)]\n target.splice(0, target.length)\n target.push(...newTarget)\n return true\n }\n\n return {\n addServer,\n deleteServer,\n }\n }\n\n /**\n * Returns mutators for a specific document by name.\n *\n * @param documentName - The name of the document to get mutators for\n * @returns An object containing mutators for requests, request examples, security schemes, environments, and cookies\n */\n const documentMutators = (documentName: string) => {\n const document = getDocument(store, documentName)\n\n if (document) {\n // Make sure the document has a servers array\n if (!document.servers) {\n document.servers = []\n }\n\n // Make sure the document has the securitySchema object\n if (!document.components) {\n document.components = {}\n }\n\n if (!document.components.securitySchemes) {\n document.components.securitySchemes = {}\n }\n }\n\n return {\n requestMutators: requestMutators(document),\n securitySchemeMutators: securitySchemeMutators(document?.components?.securitySchemes),\n serverMutators: serverMutators(document?.servers),\n }\n }\n\n /**\n * Returns mutators for the workspace-level configuration.\n *\n * @returns An object containing mutators for environments and cookies at the workspace level\n */\n const workspaceMutators = () => {\n const workspace = store.workspace\n\n // Make sure the workspace has a servers array\n if (!workspace['x-scalar-client-config-servers']) {\n workspace['x-scalar-client-config-servers'] = []\n }\n\n // Make sure the workspace has the securitySchema object\n if (!store.workspace['x-scalar-client-config-security-schemes']) {\n store.workspace['x-scalar-client-config-security-schemes'] = {}\n }\n\n return {\n serverMutators: serverMutators(store.workspace['x-scalar-client-config-servers']),\n securitySchemeMutators: securitySchemeMutators(store.workspace['x-scalar-client-config-security-schemes']),\n }\n }\n\n return {\n /**\n * Returns mutators for the workspace-level configuration.\n */\n workspace: () => workspaceMutators(),\n /**\n * Returns mutators for the currently active document.\n * Falls back to the first document if no active document is set.\n */\n active: () =>\n documentMutators(store.workspace['x-scalar-active-document'] ?? Object.keys(store.workspace.documents)[0] ?? ''),\n /**\n * Returns mutators for a specific document by name.\n *\n * @param name - The name of the document\n */\n doc: (name: string) => documentMutators(name),\n }\n}\n\nexport {\n type AuthMeta,\n deleteSecurityScheme,\n updateSecurityScheme,\n updateSelectedAuthTab,\n updateSelectedScopes,\n updateSelectedSecuritySchemes,\n} from './auth'\nexport { deleteCookie, upsertCookie } from './cookie'\nexport { createEmptyDocument, deleteDocument, toggleSecurity, updateDocumentIcon, updateWatchMode } from './document'\nexport {\n upsertEnvironment,\n upsertEnvironmentVariable,\n} from './environment'\nexport {\n type OperationExampleMeta,\n type OperationMeta,\n addOperationParameter,\n addOperationRequestBodyFormRow,\n createOperation,\n deleteAllOperationParameters,\n deleteOperation,\n deleteOperationExample,\n deleteOperationParameter,\n deleteOperationRequestBodyFormRow,\n updateOperationParameter,\n updateOperationPathMethod,\n updateOperationRequestBodyContentType,\n updateOperationRequestBodyExample,\n updateOperationRequestBodyFormRow,\n updateOperationSummary,\n} from './operation'\nexport {\n addServer,\n deleteServer,\n updateSelectedServer,\n updateServer,\n updateServerVariables,\n} from './server'\nexport {\n addTab,\n closeOtherTabs,\n closeTab,\n focusLastTab,\n focusTab,\n navigateNextTab,\n navigatePreviousTab,\n updateTabs,\n} from './tabs'\nexport { createTag, deleteTag } from './tag'\nexport { updateActiveProxy, updateColorMode, updateSelectedClient, updateTheme } from './workspace'\n"],
|
|
5
|
+
"mappings": "AACA,SAAS,mBAAmB;AAC5B,SAAS,uBAAuB;AAChC,SAAS,8BAA8B;AAWhC,SAAS,uBAAuB,OAAuB;AAO5D,QAAM,iBAAiB,CAAC,WAA4B;AAMlD,UAAMA,aAAY,CAAC,WAAkC;AACnD,UAAI,CAAC,QAAQ;AACX,eAAO;AAAA,MACT;AACA,aAAO,KAAK,MAAM;AAClB,aAAO;AAAA,IACT;AAOA,UAAMC,gBAAe,CAAC,QAAyB;AAC7C,UAAI,CAAC,QAAQ;AACX,eAAO;AAAA,MACT;AACA,YAAM,YAAY,CAAC,GAAG,OAAO,OAAO,CAAC,OAAO,GAAG,QAAQ,GAAG,CAAC;AAC3D,aAAO,OAAO,GAAG,OAAO,MAAM;AAC9B,aAAO,KAAK,GAAG,SAAS;AACxB,aAAO;AAAA,IACT;AAEA,WAAO;AAAA,MACL,WAAAD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AA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,cAAc,oBAAoB;AAC3C,SAAS,qBAAqB,gBAAgB,gBAAgB,oBAAoB,uBAAuB;AACzG;AAAA,EACE;AAAA,EACA;AAAA,OACK;AACP;AAAA,EAGE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,WAAW,iBAAiB;AACrC,SAAS,mBAAmB,iBAAiB,sBAAsB,mBAAmB;",
|
|
6
6
|
"names": ["addServer", "deleteServer"]
|
|
7
7
|
}
|
|
@@ -2,6 +2,7 @@ import type { HttpMethod } from '@scalar/helpers/http/http-methods';
|
|
|
2
2
|
import type { WorkspaceStore } from '../client.js';
|
|
3
3
|
import type { OperationEvents } from '../events/definitions/operation.js';
|
|
4
4
|
import type { WorkspaceDocument } from '../schemas.js';
|
|
5
|
+
import type { OperationObject, ParameterObject } from '../schemas/v3.1/strict/openapi-document.js';
|
|
5
6
|
/**
|
|
6
7
|
* Describes the minimal identity for an operation in the workspace document.
|
|
7
8
|
* It is used by mutators to find the target operation under `paths`.
|
|
@@ -173,6 +174,20 @@ export declare const deleteAllOperationParameters: (document: WorkspaceDocument
|
|
|
173
174
|
/** ------------------------------------------------------------------------------------------------
|
|
174
175
|
* Operation Request Body Mutators
|
|
175
176
|
* ------------------------------------------------------------------------------------------------ */
|
|
177
|
+
/**
|
|
178
|
+
* Sets a header parameter value for a specific example key.
|
|
179
|
+
* Creates the header parameter if it does not exist, otherwise updates the existing one.
|
|
180
|
+
*
|
|
181
|
+
* Note: This function does not handle parameters with content (ParameterWithContentObject).
|
|
182
|
+
* Those cases are currently unsupported and will no-op.
|
|
183
|
+
*/
|
|
184
|
+
export declare const setHeader: ({ operation, type, name, value, exampleKey, }: {
|
|
185
|
+
operation: OperationObject;
|
|
186
|
+
type: ParameterObject["in"];
|
|
187
|
+
name: string;
|
|
188
|
+
value: string;
|
|
189
|
+
exampleKey: string;
|
|
190
|
+
}) => void;
|
|
176
191
|
/**
|
|
177
192
|
* Sets the selected request-body content type for the current `exampleKey`.
|
|
178
193
|
* This stores the selection under `x-scalar-selected-content-type` on the
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"operation.d.ts","sourceRoot":"","sources":["../../src/mutators/operation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mCAAmC,CAAA;AAMnE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAA;AAC9C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAA;AAMrE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAA;
|
|
1
|
+
{"version":3,"file":"operation.d.ts","sourceRoot":"","sources":["../../src/mutators/operation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mCAAmC,CAAA;AAMnE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAA;AAC9C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAA;AAMrE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAA;AAElD,OAAO,KAAK,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,wCAAwC,CAAA;AAI9F;;;;;;;;GAQG;AACH,MAAM,MAAM,aAAa,GAAG;IAC1B,MAAM,EAAE,UAAU,CAAA;IAClB,IAAI,EAAE,MAAM,CAAA;CACb,CAAA;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,MAAM,oBAA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izedPath);
|
|
91
91
|
preventPollution(method);
|
|
92
92
|
document.paths[normalizedPath][method] = operation;
|
|
93
|
+
const { servers } = operation;
|
|
94
|
+
const firstServer = unpackProxyObject(servers?.[0]);
|
|
95
|
+
for (const server of servers ?? []) {
|
|
96
|
+
if (!document.servers?.some((s) => s.url === server.url)) {
|
|
97
|
+
if (!document.servers) {
|
|
98
|
+
document.servers = [];
|
|
99
|
+
}
|
|
100
|
+
document.servers.push(unpackProxyObject(server));
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (firstServer) {
|
|
104
|
+
document["x-scalar-selected-server"] = firstServer.url;
|
|
105
|
+
}
|
|
93
106
|
payload.callback?.(true);
|
|
94
107
|
return normalizedPath;
|
|
95
108
|
};
|
|
@@ -319,6 +332,46 @@ const deleteAllOperationParameters = (document, { meta, type }) => {
|
|
|
319
332
|
}
|
|
320
333
|
operation.parameters = operation.parameters?.filter((it) => getResolvedRef(it).in !== type) ?? [];
|
|
321
334
|
};
|
|
335
|
+
const setHeader = ({
|
|
336
|
+
operation,
|
|
337
|
+
type,
|
|
338
|
+
name,
|
|
339
|
+
value,
|
|
340
|
+
exampleKey
|
|
341
|
+
}) => {
|
|
342
|
+
if (!operation.parameters) {
|
|
343
|
+
operation.parameters = [];
|
|
344
|
+
}
|
|
345
|
+
const existingParameter = operation.parameters.find((param) => {
|
|
346
|
+
const resolvedParam = getResolvedRef(param);
|
|
347
|
+
return resolvedParam.name.toLowerCase() === name.toLowerCase() && resolvedParam.in === type;
|
|
348
|
+
});
|
|
349
|
+
if (!existingParameter) {
|
|
350
|
+
operation.parameters.push({
|
|
351
|
+
in: type,
|
|
352
|
+
name,
|
|
353
|
+
examples: {
|
|
354
|
+
[exampleKey]: {
|
|
355
|
+
value
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
const resolvedParameter = getResolvedRef(existingParameter);
|
|
362
|
+
if (isContentTypeParameterObject(resolvedParameter)) {
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
if (!resolvedParameter.examples) {
|
|
366
|
+
resolvedParameter.examples = {};
|
|
367
|
+
}
|
|
368
|
+
if (!resolvedParameter.examples[exampleKey]) {
|
|
369
|
+
resolvedParameter.examples[exampleKey] = {};
|
|
370
|
+
}
|
|
371
|
+
getResolvedRef(resolvedParameter.examples[exampleKey]).value = value;
|
|
372
|
+
return;
|
|
373
|
+
};
|
|
374
|
+
const SKIP_CONTENT_TYPE_HEADERS = ["other", "none"];
|
|
322
375
|
const updateOperationRequestBodyContentType = (document, { meta, payload }) => {
|
|
323
376
|
if (!document) {
|
|
324
377
|
return;
|
|
@@ -338,6 +391,15 @@ const updateOperationRequestBodyContentType = (document, { meta, payload }) => {
|
|
|
338
391
|
requestBody["x-scalar-selected-content-type"] = {};
|
|
339
392
|
}
|
|
340
393
|
requestBody["x-scalar-selected-content-type"][meta.exampleKey] = payload.contentType;
|
|
394
|
+
if (!SKIP_CONTENT_TYPE_HEADERS.includes(payload.contentType)) {
|
|
395
|
+
setHeader({
|
|
396
|
+
operation,
|
|
397
|
+
name: "Content-Type",
|
|
398
|
+
type: "header",
|
|
399
|
+
exampleKey: meta.exampleKey,
|
|
400
|
+
value: payload.contentType
|
|
401
|
+
});
|
|
402
|
+
}
|
|
341
403
|
};
|
|
342
404
|
const updateOperationRequestBodyExample = (document, { meta, payload, contentType }) => {
|
|
343
405
|
if (!document) {
|
|
@@ -487,6 +549,7 @@ export {
|
|
|
487
549
|
deleteOperationExample,
|
|
488
550
|
deleteOperationParameter,
|
|
489
551
|
deleteOperationRequestBodyFormRow,
|
|
552
|
+
setHeader,
|
|
490
553
|
updateOperationParameter,
|
|
491
554
|
updateOperationPathMethod,
|
|
492
555
|
updateOperationRequestBodyContentType,
|