Skip to content

Commit

Permalink
refactor(oas): export and reuse Variables type (#897)
Browse files Browse the repository at this point in the history
  • Loading branch information
kanadgupta authored Sep 4, 2024
1 parent fa62012 commit 95423ff
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
8 changes: 3 additions & 5 deletions packages/oas/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ interface PathMatch {
}
type PathMatches = PathMatch[];

type Variables = Record<string, { default?: number | string }[] | number | string | { default?: number | string }>;

const SERVER_VARIABLE_REGEX = /{([-_a-zA-Z0-9:.[\]]+)}/g;

function ensureProtocol(url: string) {
Expand Down Expand Up @@ -328,7 +326,7 @@ export default class Oas {
return this.api;
}

url(selected = 0, variables?: Variables) {
url(selected = 0, variables?: RMOAS.ServerVariable) {
const url = normalizedUrl(this.api, selected);
return this.replaceUrl(url, variables || this.defaultVariables(selected)).trim();
}
Expand All @@ -347,7 +345,7 @@ export default class Oas {

defaultVariables(selected = 0) {
const variables = this.variables(selected);
const defaults: Record<string, unknown> = {};
const defaults: RMOAS.ServerVariable = {};

Object.keys(variables).forEach(key => {
defaults[key] = getUserVariable(this.user, key) || variables[key].default || '';
Expand Down Expand Up @@ -484,7 +482,7 @@ export default class Oas {
* @param url A URL to swap variables into.
* @param variables An object containing variables to swap into the URL.
*/
replaceUrl(url: string, variables: Variables = {}) {
replaceUrl(url: string, variables: RMOAS.ServerVariable = {}) {
// When we're constructing URLs, server URLs with trailing slashes cause problems with doing
// lookups, so if we have one here on, slice it off.
return stripTrailingSlash(
Expand Down
2 changes: 1 addition & 1 deletion packages/oas/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface DataForHAR {
query?: Record<string, any>;
server?: {
selected: number;
variables?: Record<string, unknown>;
variables?: ServerVariable;
};
}

Expand Down

0 comments on commit 95423ff

Please sign in to comment.