Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/client/interfaces/Operation.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export interface Operation extends OperationParameters {
errors: OperationError[];
results: OperationResponse[];
responseHeader: string | null;
server?: string;
}
1 change: 1 addition & 0 deletions src/client/interfaces/Service.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export interface Service {
name: string;
operations: Operation[];
imports: string[];
server?: string;
}
3 changes: 3 additions & 0 deletions src/openApi/v3/parser/getOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getOperationRequestBody } from './getOperationRequestBody';
import { getOperationResponseHeader } from './getOperationResponseHeader';
import { getOperationResponses } from './getOperationResponses';
import { getOperationResults } from './getOperationResults';
import { getOperationServer } from './getServer';
import { getRef } from './getRef';
import { getServiceName } from './getServiceName';
import { sortByRequired } from './sortByRequired';
Expand All @@ -24,6 +25,7 @@ export const getOperation = (
): Operation => {
const serviceName = getServiceName(tag);
const operationName = getOperationName(url, method, op.operationId);
const server = getOperationServer(op);

// Create a new operation object for this method.
const operation: Operation = {
Expand All @@ -34,6 +36,7 @@ export const getOperation = (
deprecated: op.deprecated === true,
method: method.toUpperCase(),
path: url,
server: server,
parameters: [...pathParams.parameters],
parametersPath: [...pathParams.parametersPath],
parametersQuery: [...pathParams.parametersQuery],
Expand Down
49 changes: 48 additions & 1 deletion src/openApi/v3/parser/getServer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getServer } from './getServer';
import { getOperationServer, getPathItemServer, getServer } from "./getServer";

describe('getServer', () => {
it('should produce correct result', () => {
Expand Down Expand Up @@ -44,4 +44,51 @@ describe('getServer', () => {
})
).toEqual('https://localhost:8080/api');
});

it('should produce correct result with Path Item servers', () => {
expect(
getPathItemServer({
servers: [
{
url: 'https://sub.localhost:8080/api',
},
],
})
).toEqual('https://sub.localhost:8080/api');
});

it('should produce undefined with no Path Item servers', () => {
expect(
getPathItemServer({})
).toEqual(undefined);
});

it('should produce correct result with Operation servers', () => {
expect(
getOperationServer({
servers: [
{
url: 'https://sub.localhost:8080/api',
},
],
responses: {
default: {
description: 'dummy',
}
},
})
).toEqual('https://sub.localhost:8080/api');
});

it('should produce undefined with no Operation servers', () => {
expect(
getOperationServer({
responses: {
default: {
description: 'dummy',
}
},
})
).toEqual(undefined);
});
});
17 changes: 13 additions & 4 deletions src/openApi/v3/parser/getServer.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import type { OpenApi } from '../interfaces/OpenApi';
import type { OpenApiServer } from '../interfaces/OpenApiServer';
import type { OpenApiPath } from '../interfaces/OpenApiPath';
import type { OpenApiOperation } from '../interfaces/OpenApiOperation';

export const getServer = (openApi: OpenApi): string => {
const server = openApi.servers?.[0];
const variables = server?.variables || {};
let url = server?.url || '';
export const getServerUrl = (server?: OpenApiServer): string | undefined => {
if (!server) return undefined;

const variables = server.variables || {};
let url = server.url || '';
for (const variable in variables) {
if (variables.hasOwnProperty(variable)) {
url = url.replace(`{${variable}}`, variables[variable].default);
}
}
return url.replace(/\/$/g, '');
};

export const getServer = (openApi: OpenApi): string => getServerUrl(openApi.servers?.[0]) ?? '';
export const getPathItemServer = (pathItem: OpenApiPath): string | undefined => getServerUrl(pathItem.servers?.[0]);
export const getOperationServer = (operation: OpenApiOperation): string | undefined =>
getServerUrl(operation.servers?.[0]);
2 changes: 2 additions & 0 deletions src/openApi/v3/parser/getServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { unique } from '../../../utils/unique';
import type { OpenApi } from '../interfaces/OpenApi';
import { getOperation } from './getOperation';
import { getOperationParameters } from './getOperationParameters';
import { getPathItemServer } from './getServer';

/**
* Get the OpenAPI services
Expand Down Expand Up @@ -43,6 +44,7 @@ export const getServices = (openApi: OpenApi): Service[] => {
// Push the operation in the service
service.operations.push(operation);
service.imports.push(...operation.imports);
service.server ??= getPathItemServer(path);
services.set(operation.service, service);
});
break;
Expand Down
1 change: 1 addition & 0 deletions src/templates/core/ApiRequestOptions.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export type ApiRequestOptions = {
readonly method: 'GET' | 'PUT' | 'POST' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'PATCH';
readonly url: string;
readonly server?: string;
readonly path?: Record<string, any>;
readonly cookies?: Record<string, any>;
readonly headers?: Record<string, any>;
Expand Down
2 changes: 1 addition & 1 deletion src/templates/core/functions/getUrl.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const getUrl = (config: OpenAPIConfig, options: ApiRequestOptions): string => {
return substring;
});

const url = `${config.BASE}${path}`;
const url = `${options.server || config.BASE}${path}`;
if (options.query) {
return `${url}${getQueryString(options.query)}`;
}
Expand Down
5 changes: 5 additions & 0 deletions src/templates/exportService.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ export class {{{name}}}{{{@root.postfix}}} {
{{/if}}
method: '{{{method}}}',
url: '{{{path}}}',
{{#if server}}
server: '{{{server}}}',
{{else if ../server}}
server: '{{{../server}}}',
{{/if}}
{{#if parametersPath}}
path: {
{{#each parametersPath}}
Expand Down
Loading