- Working HttpClient exporter

This commit is contained in:
Ferdi Koomen 2022-01-25 13:34:23 +01:00
parent 83a0692b10
commit e1268b197a
16 changed files with 54 additions and 34 deletions

View File

@ -3,6 +3,7 @@
import type { BaseHttpRequest } from './core/BaseHttpRequest';
import type { OpenAPIConfig } from './core/OpenAPI';
import { {{{httpRequest}}} } from './core/{{{httpRequest}}}';
{{#if services}}
{{#each services}}
import { {{{name}}}{{{@root.postfix}}} } from './services/{{{name}}}{{{@root.postfix}}}';
@ -17,7 +18,7 @@ export class {{{clientName}}} {
public readonly {{{camelCase name}}}: {{{name}}}{{{@root.postfix}}};
{{/each}}
public readonly request: BaseHttpRequest;
private readonly request: BaseHttpRequest;
constructor(config?: OpenAPIConfig, HttpRequest: HttpRequestConstructor = {{{httpRequest}}}) {
this.request = new HttpRequest({

View File

@ -1,3 +1,18 @@
export const getHttpRequestName = (): string => {
return '';
import { HttpClient } from '../HttpClient';
/**
* Generate the HttpRequest filename based on the selected client
* @param httpClient The selected httpClient (fetch, xhr, node or axios)
*/
export const getHttpRequestName = (httpClient: HttpClient): string => {
switch (httpClient) {
case HttpClient.FETCH:
return 'FetchHttpRequest';
case HttpClient.XHR:
return 'XHRHttpRequest';
case HttpClient.NODE:
return 'NodeHttpRequest';
case HttpClient.AXIOS:
return 'AxiosHttpRequest';
}
};

View File

@ -2,9 +2,9 @@ import camelCase from 'camelcase';
import Handlebars from 'handlebars/runtime';
import { EOL } from 'os';
import { Enum } from '../client/interfaces/Enum';
import { Model } from '../client/interfaces/Model';
import { HttpClient } from '../HttpClient';
import type { Enum } from '../client/interfaces/Enum';
import type { Model } from '../client/interfaces/Model';
import type { HttpClient } from '../HttpClient';
import { unique } from './unique';
export const registerHandlebarHelpers = (root: {

View File

@ -2,7 +2,7 @@ import type { Client } from '../client/interfaces/Client';
import { HttpClient } from '../HttpClient';
import { Indent } from '../Indent';
import { mkdir, rmdir, writeFile } from './fileSystem';
import { Templates } from './registerHandlebarTemplates';
import type { Templates } from './registerHandlebarTemplates';
import { writeClient } from './writeClient';
jest.mock('./fileSystem');

View File

@ -1,12 +1,12 @@
import { resolve } from 'path';
import type { Client } from '../client/interfaces/Client';
import { HttpClient } from '../HttpClient';
import { Indent } from '../Indent';
import type { HttpClient } from '../HttpClient';
import type { Indent } from '../Indent';
import { mkdir, rmdir } from './fileSystem';
import { isDefined } from './isDefined';
import { isSubDirectory } from './isSubdirectory';
import { Templates } from './registerHandlebarTemplates';
import type { Templates } from './registerHandlebarTemplates';
import { writeClientClass } from './writeClientClass';
import { writeClientCore } from './writeClientCore';
import { writeClientIndex } from './writeClientIndex';

View File

@ -2,7 +2,7 @@ import type { Client } from '../client/interfaces/Client';
import { HttpClient } from '../HttpClient';
import { Indent } from '../Indent';
import { mkdir, rmdir, writeFile } from './fileSystem';
import { Templates } from './registerHandlebarTemplates';
import type { Templates } from './registerHandlebarTemplates';
import { writeClientClass } from './writeClientClass';
jest.mock('./fileSystem');

View File

@ -1,14 +1,16 @@
import { resolve } from 'path';
import type { Client } from '../client/interfaces/Client';
import { HttpClient } from '../HttpClient';
import { Indent } from '../Indent';
import type { HttpClient } from '../HttpClient';
import type { Indent } from '../Indent';
import { writeFile } from './fileSystem';
import { formatCode as f } from './formatCode';
import { formatIndentation as i } from './formatIndentation';
import { Templates } from './registerHandlebarTemplates';
import { getHttpRequestName } from './getHttpRequestName';
import type { Templates } from './registerHandlebarTemplates';
import { sortModelsByName } from './sortModelsByName';
import { sortServicesByName } from './sortServicesByName';
/**
* Generate the OpenAPI client index file using the Handlebar template and write it to disk.
* The index file just contains all the exports you need to use the client as a standalone
@ -37,7 +39,7 @@ export const writeClientClass = async (
version: client.version,
models: sortModelsByName(client.models),
services: sortServicesByName(client.services),
httpRequest: 'XHRHttpRequest',
httpRequest: getHttpRequestName(httpClient),
});
await writeFile(resolve(outputPath, 'client.ts'), i(f(templateResult), indent));

View File

@ -2,7 +2,7 @@ import type { Client } from '../client/interfaces/Client';
import { HttpClient } from '../HttpClient';
import { Indent } from '../Indent';
import { writeFile } from './fileSystem';
import { Templates } from './registerHandlebarTemplates';
import type { Templates } from './registerHandlebarTemplates';
import { writeClientCore } from './writeClientCore';
jest.mock('./fileSystem');

View File

@ -1,12 +1,13 @@
import { resolve } from 'path';
import type { Client } from '../client/interfaces/Client';
import { HttpClient } from '../HttpClient';
import { Indent } from '../Indent';
import type { HttpClient } from '../HttpClient';
import type { Indent } from '../Indent';
import { copyFile, exists, writeFile } from './fileSystem';
import { formatIndentation as i } from './formatIndentation';
import { getHttpRequestName } from './getHttpRequestName';
import { isDefined } from './isDefined';
import { Templates } from './registerHandlebarTemplates';
import type { Templates } from './registerHandlebarTemplates';
/**
* Generate OpenAPI core files, this includes the basic boilerplate code to handle requests.
@ -26,12 +27,13 @@ export const writeClientCore = async (
clientName?: string,
request?: string
): Promise<void> => {
const httpRequest = getHttpRequestName(httpClient);
const context = {
httpClient,
clientName,
httpRequest,
server: client.server,
version: client.version,
httpRequest: 'XHRHttpRequest',
};
await writeFile(resolve(outputPath, 'OpenAPI.ts'), i(templates.core.settings(context), indent));
@ -43,7 +45,7 @@ export const writeClientCore = async (
if (isDefined(clientName)) {
await writeFile(resolve(outputPath, 'BaseHttpRequest.ts'), i(templates.core.baseHttpRequest(context), indent));
await writeFile(resolve(outputPath, 'XHRHttpRequest.ts'), i(templates.core.httpRequest(context), indent));
await writeFile(resolve(outputPath, `${httpRequest}.ts`), i(templates.core.httpRequest(context), indent));
}
if (request) {

View File

@ -1,6 +1,6 @@
import type { Client } from '../client/interfaces/Client';
import { writeFile } from './fileSystem';
import { Templates } from './registerHandlebarTemplates';
import type { Templates } from './registerHandlebarTemplates';
import { writeClientIndex } from './writeClientIndex';
jest.mock('./fileSystem');

View File

@ -2,7 +2,7 @@ import type { Model } from '../client/interfaces/Model';
import { HttpClient } from '../HttpClient';
import { Indent } from '../Indent';
import { writeFile } from './fileSystem';
import { Templates } from './registerHandlebarTemplates';
import type { Templates } from './registerHandlebarTemplates';
import { writeClientModels } from './writeClientModels';
jest.mock('./fileSystem');

View File

@ -1,12 +1,12 @@
import { resolve } from 'path';
import type { Model } from '../client/interfaces/Model';
import { HttpClient } from '../HttpClient';
import { Indent } from '../Indent';
import type { HttpClient } from '../HttpClient';
import type { Indent } from '../Indent';
import { writeFile } from './fileSystem';
import { formatCode as f } from './formatCode';
import { formatIndentation as i } from './formatIndentation';
import { Templates } from './registerHandlebarTemplates';
import type { Templates } from './registerHandlebarTemplates';
/**
* Generate Models using the Handlebar template and write to disk.

View File

@ -2,7 +2,7 @@ import type { Model } from '../client/interfaces/Model';
import { HttpClient } from '../HttpClient';
import { Indent } from '../Indent';
import { writeFile } from './fileSystem';
import { Templates } from './registerHandlebarTemplates';
import type { Templates } from './registerHandlebarTemplates';
import { writeClientSchemas } from './writeClientSchemas';
jest.mock('./fileSystem');

View File

@ -1,12 +1,12 @@
import { resolve } from 'path';
import type { Model } from '../client/interfaces/Model';
import { HttpClient } from '../HttpClient';
import { Indent } from '../Indent';
import type { HttpClient } from '../HttpClient';
import type { Indent } from '../Indent';
import { writeFile } from './fileSystem';
import { formatCode as f } from './formatCode';
import { formatIndentation as i } from './formatIndentation';
import { Templates } from './registerHandlebarTemplates';
import type { Templates } from './registerHandlebarTemplates';
/**
* Generate Schemas using the Handlebar template and write to disk.

View File

@ -2,7 +2,7 @@ import type { Service } from '../client/interfaces/Service';
import { HttpClient } from '../HttpClient';
import { Indent } from '../Indent';
import { writeFile } from './fileSystem';
import { Templates } from './registerHandlebarTemplates';
import type { Templates } from './registerHandlebarTemplates';
import { writeClientServices } from './writeClientServices';
jest.mock('./fileSystem');

View File

@ -1,12 +1,12 @@
import { resolve } from 'path';
import type { Service } from '../client/interfaces/Service';
import { HttpClient } from '../HttpClient';
import { Indent } from '../Indent';
import type { HttpClient } from '../HttpClient';
import type { Indent } from '../Indent';
import { writeFile } from './fileSystem';
import { formatCode as f } from './formatCode';
import { formatIndentation as i } from './formatIndentation';
import { Templates } from './registerHandlebarTemplates';
import type { Templates } from './registerHandlebarTemplates';
const VERSION_TEMPLATE_STRING = 'OpenAPI.VERSION';