Skip to content

Commit

Permalink
updating caller
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbarbour committed Nov 1, 2023
1 parent b3116a6 commit 02f8406
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"build": "tsc --project tsconfig.json",
"test": "jest --ci --runInBand --coverage --reporters=default --reporters=jest-junit --passWithNoTests",
"lint": "eslint **/*.ts",
"start": "ts-node ./src/cli.ts generate -h $(pwd)/test/schema-example.ts v=1.0.0"
"prebuild": "ts-node ./src/cli.ts generate -h $(pwd)/test/schema-example.ts v=1.0.0"
},
"eslintConfig": {
"extends": [
Expand Down
45 changes: 29 additions & 16 deletions src/sdk-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,18 @@ function sdkMethod(version: string | undefined, path: string, method: string, pa
const resource = '${path}';
const path = \`${resourcePath}\`;
const versionedHeaders = ${version ? `{...headers, ['X-API-VERSION']: '${version}' }` : 'headers'};
const result = await this.caller.call('${method.toUpperCase()}', resource, path, ${bodyParam ? 'JSON.stringify(body)' : 'undefined'}, ${pathParams ? 'params' : '{}'}, ${queryParams ? 'queryParameters' : '{}'}, ${multiQueryParams ? 'multiQueryParameters' : '{}'}, versionedHeaders, this.server());
const props: CallerProps = {
method: '${method.toUpperCase()}',
resource,
path,
body: ${bodyParam ? 'JSON.stringify(body)' : 'undefined'},
pathParameters: ${pathParams ? 'params' : '{}'},
queryParameters: ${queryParams ? 'queryParameters' : '{}'},
multiQueryParameters: ${multiQueryParams ? 'multiQueryParameters' : '{}'},
headers: versionedHeaders,
uri: this.server()
}
const result = await this.caller.call(props);
${version ? versionCheck : ''}${bodyValue(oas, methodDefinition)}
throw new Error(\`Unknown status \${result.statusCode} returned from \${path}\`)
}`;
Expand Down Expand Up @@ -180,30 +191,32 @@ export function generateSdkFrom(oas: OAS, version?: string): string {

return `import { ${[...new Set([...imports])].join(', ')} } from './model';
export type CallerProps = {
method: string;
resource: string;
path: string;
body?: string;
pathParameters: Record<string, string>;
queryParameters: Record<string, string>;
multiQueryParameters: Record<string, string[]>;
headers: Record<string, string>;
uri: string;
}
export type Caller = {
call(
method: string,
resource: string,
path: string,
body: string | undefined,
pathParameters: Record<string, string>,
queryParameters: Record<string, string>,
multiQueryParameters: Record<string, string[]>,
headers: Record<string, string>,
uri?: string
): Promise<{ statusCode: number; body: string; headers: Record<string, string> }>;
}
call(props: CallerProps): Promise<{ statusCode: number; body: string; headers: Record<string, string> }>;
}
export class ${name} {
constructor(
private readonly caller: Caller,${servers ? `\n private readonly serverLookup?: {${serverLookupTyped.join('; ')}},\n public readonly servers: Array<{url: string; variables: Record<string, string>}> = [${servers.join(', ')}]` : ''}
){}
private server(): string | undefined {
if(this.servers.length === 0) return undefined;
private server(): string {
if(this.servers.length === 0) return '';
const server = !this.serverLookup ? this.servers[0] : this.servers.find(it => Object.keys(this.serverLookup!).reduce((result, key) => result && (!(this.serverLookup as any)[key] || it.variables[key] === (this.serverLookup as any)[key]), true as boolean));
return server && Object.keys(server.variables).reduce((url, key) => url.replace(\`{\${key}}\`, server.variables[key]), server.url);
return server ? Object.keys(server.variables).reduce((url, key) => url.replace(\`{\${key}}\`, server.variables[key]), server.url): '';
}
${sdkMethods.join('\n\n')}
Expand Down
16 changes: 16 additions & 0 deletions test/sdk.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Caller, CallerProps, ChickenStoreAPISdk } from '../generated/chicken-store-api/sdk';


const caller: Caller = {
async call(props: CallerProps): Promise<{ statusCode: number; body: string; headers: Record<string, string> }> {
return {statusCode: 200, body: '[]', headers: { 'x-uri': props.uri }};
}
}

describe('SDK', () => {

it('should', async () => {
const result = await new ChickenStoreAPISdk(caller, {environment: 'prod'}).getChicken();
expect(result.headers['x-uri']).toEqual('https://api.xyz.io/views')
})
});

0 comments on commit 02f8406

Please sign in to comment.