UNPKG

@scalar/api-client

Version:

the open source API testing client

108 lines (107 loc) 3.15 kB
import { isObject as p } from "@scalar/helpers/object/is-object"; import { objectEntries as x } from "@scalar/helpers/object/object-entries"; const u = { "x-scalar-secret-client-id": "x-scalar-client-id", "x-scalar-secret-client-secret": "clientSecret", "x-scalar-secret-password": "password", "x-scalar-secret-redirect-uri": "x-scalar-redirect-uri", "x-scalar-secret-token": "token", "x-scalar-secret-username": "username" }, n = (t, c, i = {}) => Object.fromEntries( t.map((a) => { const r = i[a] || c[a] || c[u[a]] || ""; return [a, r]; }) ), d = (t, c) => { const i = /* @__PURE__ */ new Set(); return { flows: x(t ?? {}).reduce((r, [e, s]) => { if (!p(s)) return r; const o = s.selectedScopes; return Array.isArray(o) && o.forEach((l) => typeof l == "string" && i.add(l)), e === "implicit" && (r.implicit = { ...s, ...n( ["x-scalar-secret-client-id", "x-scalar-secret-redirect-uri", "x-scalar-secret-token"], s, c?.implicit ) }), e === "password" && (r[e] = { ...s, ...n( [ "x-scalar-secret-client-id", "x-scalar-secret-client-secret", "x-scalar-secret-username", "x-scalar-secret-password", "x-scalar-secret-token" ], s, c?.password ) }), e === "clientCredentials" && (r[e] = { ...s, ...n( ["x-scalar-secret-client-id", "x-scalar-secret-client-secret", "x-scalar-secret-token"], s, c?.clientCredentials ) }), e === "authorizationCode" && (r[e] = { ...s, ...n( [ "x-scalar-secret-client-id", "x-scalar-secret-client-secret", "x-scalar-secret-redirect-uri", "x-scalar-secret-token" ], s, c?.authorizationCode ) }), r; }, {}), selectedScopes: Array.from(i) }; }, w = (t, c, i, a) => { const r = c.getAuthSecrets(a, i); if (t.type === "apiKey") { const e = r?.type === "apiKey" ? r : void 0; return { ...t, "x-scalar-secret-token": e?.["x-scalar-secret-token"] || t.value || "" }; } if (t.type === "http") { const e = r?.type === "http" ? r : void 0; return { ...t, "x-scalar-secret-token": e?.["x-scalar-secret-token"] || t.token || "", "x-scalar-secret-username": e?.["x-scalar-secret-username"] || t.username || "", "x-scalar-secret-password": e?.["x-scalar-secret-password"] || t.password || "" }; } if (t.type === "oauth2") { const e = r?.type === "oauth2" ? r : void 0, s = d(t.flows, e); return { ...t, flows: s.flows, "x-default-scopes": s.selectedScopes }; } if (t.type === "openIdConnect") { const e = r?.type === "openIdConnect" ? r : void 0, s = d( { implicit: e?.implicit, password: e?.password, clientCredentials: e?.clientCredentials, authorizationCode: e?.authorizationCode }, e ); return { ...t, ...x(s.flows).length ? { flows: s.flows } : {} }; } return t; }; export { w as extractSecuritySchemeSecrets };