@scalar/oas-utils
Version:
Open API spec and Yaml handling utilities
106 lines (105 loc) • 2.77 kB
JavaScript
const migrateSecurityScheme = (scheme, auth) => {
if (scheme.type === "apiKey" && auth.type === "apiKey") {
return {
...scheme,
value: auth.value
};
}
if (scheme.type === "http" && auth.type === "http") {
return {
...scheme,
username: auth.username,
password: auth.password,
token: auth.token
};
}
if (scheme.type === "oauth2") {
const { flow, ..._scheme } = scheme;
if (flow.type === "implicit" && auth.type === "oauth-implicit") {
return {
..._scheme,
flows: {
implicit: {
...flow,
"scopes": flow.scopes,
"token": auth.token,
"x-scalar-client-id": _scheme["x-scalar-client-id"]
}
}
};
}
if (flow.type === "password" && auth.type === "oauth-password") {
return {
..._scheme,
flows: {
password: {
...flow,
"username": auth.username,
"password": auth.password,
"token": auth.token,
"clientSecret": auth.clientSecret,
"scopes": flow.scopes,
"x-scalar-client-id": _scheme["x-scalar-client-id"]
}
}
};
}
if (flow.type === "clientCredentials" && auth.type === "oauth-clientCredentials") {
return {
..._scheme,
flows: {
clientCredentials: {
...flow,
"token": auth.token,
"clientSecret": auth.clientSecret,
"scopes": flow.scopes,
"x-scalar-client-id": _scheme["x-scalar-client-id"]
}
}
};
}
if (flow.type === "authorizationCode" && auth.type === "oauth-authorizationCode") {
return {
..._scheme,
flows: {
authorizationCode: {
...flow,
"token": auth.token,
"clientSecret": auth.clientSecret,
"scopes": flow.scopes,
"x-scalar-client-id": _scheme["x-scalar-client-id"]
}
}
};
}
}
return null;
};
const migrate_v_2_2_0 = (data) => {
console.info("Performing data migration v-2.1.0 to v-2.2.0");
const securitySchemes = Object.values(data.securitySchemes).reduce(
(prev, s) => {
const collection = Object.values(data.collections).find((c) => c.securitySchemes.includes(s.uid));
const auth = collection?.auth?.[s.uid];
if (!auth) {
return prev;
}
const newScheme = migrateSecurityScheme(s, auth);
if (newScheme) {
prev[s.uid] = newScheme;
}
return prev;
},
{}
);
const servers = data.servers;
return {
...data,
securitySchemes,
servers
};
};
export {
migrate_v_2_2_0
};
//# sourceMappingURL=migration.js.map