adjust frontend to new backend
This commit is contained in:
35
templates/services/ObjectParamAPI.mustache
Normal file
35
templates/services/ObjectParamAPI.mustache
Normal file
@@ -0,0 +1,35 @@
|
||||
import type { HttpFile } from '../http/http';
|
||||
import type { Configuration } from '../configuration'
|
||||
import type * as req from "../types/ObjectParamAPI";
|
||||
{{#useRxJS}}
|
||||
import type { Observable } from 'rxjs';
|
||||
{{/useRxJS}}
|
||||
|
||||
{{#models}}
|
||||
{{#model}}
|
||||
import type { {{{ classname }}} } from '{{{ importPath }}}';
|
||||
{{/model}}
|
||||
{{/models}}
|
||||
{{#apiInfo}}
|
||||
{{#apis}}
|
||||
{{#operations}}
|
||||
|
||||
|
||||
export abstract class AbstractObject{{classname}} {
|
||||
{{#operation}}
|
||||
/**
|
||||
{{#notes}}
|
||||
* {{¬es}}
|
||||
{{/notes}}
|
||||
{{#summary}}
|
||||
* {{&summary}}
|
||||
{{/summary}}
|
||||
* @param param the request object
|
||||
*/
|
||||
public abstract {{nickname}}(param: req.{{classname}}{{operationIdCamelCase}}Request, options?: Configuration): {{#useRxJS}}Observable{{/useRxJS}}{{^useRxJS}}Promise{{/useRxJS}}<{{{returnType}}}{{^returnType}}void{{/returnType}}>;
|
||||
|
||||
{{/operation}}
|
||||
}
|
||||
{{/operations}}
|
||||
{{/apis}}
|
||||
{{/apiInfo}}
|
||||
23
templates/services/ObservableAPI.mustache
Normal file
23
templates/services/ObservableAPI.mustache
Normal file
@@ -0,0 +1,23 @@
|
||||
import type { HttpFile } from "../http/http";
|
||||
import type { Observable } from {{#useRxJS}}"rxjs"{{/useRxJS}}{{^useRxJS}}"../rxjsStub"{{/useRxJS}};
|
||||
import type { Configuration } from "../configuration";
|
||||
|
||||
{{#models}}
|
||||
{{#model}}
|
||||
import { {{{ classname }}} } from "{{{ importPath }}}";
|
||||
{{/model}}
|
||||
{{/models}}
|
||||
{{#apiInfo}}
|
||||
{{#apis}}
|
||||
{{#operations}}
|
||||
|
||||
|
||||
export abstract class AbstractObservable{{classname}} {
|
||||
{{#operation}}
|
||||
public abstract {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}options?: Configuration): Observable<{{{returnType}}}{{^returnType}}void{{/returnType}}>;
|
||||
|
||||
{{/operation}}
|
||||
}
|
||||
{{/operations}}
|
||||
{{/apis}}
|
||||
{{/apiInfo}}
|
||||
22
templates/services/PromiseAPI.mustache
Normal file
22
templates/services/PromiseAPI.mustache
Normal file
@@ -0,0 +1,22 @@
|
||||
import type { HttpFile } from "../http/http";
|
||||
import type { Configuration } from "../configuration";
|
||||
|
||||
{{#models}}
|
||||
{{#model}}
|
||||
import { {{{ classname }}} } from "{{{ importPath }}}";
|
||||
{{/model}}
|
||||
{{/models}}
|
||||
{{#apiInfo}}
|
||||
{{#apis}}
|
||||
{{#operations}}
|
||||
|
||||
|
||||
export abstract class AbstractPromise{{classname}} {
|
||||
{{#operation}}
|
||||
public abstract {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}options?: Configuration): Promise<{{{returnType}}}{{^returnType}}void{{/returnType}}>;
|
||||
|
||||
{{/operation}}
|
||||
}
|
||||
{{/operations}}
|
||||
{{/apis}}
|
||||
{{/apiInfo}}
|
||||
23
templates/services/api.mustache
Normal file
23
templates/services/api.mustache
Normal file
@@ -0,0 +1,23 @@
|
||||
import type { Configuration } from "../configuration";
|
||||
import type { HttpFile, RequestContext, ResponseContext } from "../http/http";
|
||||
|
||||
{{#imports}}
|
||||
import { {{classname}} } from "{{filename}}";
|
||||
{{/imports}}
|
||||
{{#operations}}
|
||||
|
||||
export abstract class Abstract{{classname}}RequestFactory {
|
||||
{{#operation}}
|
||||
public abstract {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}options?: Configuration): Promise<RequestContext>;
|
||||
|
||||
{{/operation}}
|
||||
}
|
||||
|
||||
|
||||
export abstract class Abstract{{classname}}ResponseProcessor {
|
||||
{{#operation}}
|
||||
public abstract {{nickname}}(response: ResponseContext): Promise<{{{returnType}}} {{^returnType}}void{{/returnType}}>;
|
||||
|
||||
{{/operation}}
|
||||
}
|
||||
{{/operations}}
|
||||
21
templates/services/configuration.mustache
Normal file
21
templates/services/configuration.mustache
Normal file
@@ -0,0 +1,21 @@
|
||||
import type { AbstractServerConfiguration } from "./http";
|
||||
import type { HttpLibrary, RequestContext } from "../http/http";
|
||||
import type { Middleware } from "../middleware";
|
||||
import type { AuthMethods, TokenProvider } from "../auth/auth";
|
||||
import type { Configuration } from "../configuration";
|
||||
|
||||
export abstract class AbstractConfiguration implements Configuration {
|
||||
abstract get baseServer(): AbstractServerConfiguration;
|
||||
abstract get httpApi(): HttpLibrary;
|
||||
abstract get middleware(): Middleware[];
|
||||
abstract get authMethods(): AuthMethods;
|
||||
}
|
||||
|
||||
export abstract class AbstractAuthMethod {
|
||||
public abstract getName(): string;
|
||||
public abstract applySecurityAuthentication(context: RequestContext): void | Promise<void>;
|
||||
};
|
||||
|
||||
export abstract class AbstractTokenProvider implements TokenProvider {
|
||||
public abstract getToken(): string | Promise<string>;
|
||||
}
|
||||
19
templates/services/http.mustache
Normal file
19
templates/services/http.mustache
Normal file
@@ -0,0 +1,19 @@
|
||||
{{#useRxJS}}
|
||||
import type { Observable } from "rxjs";
|
||||
{{/useRxJS}}
|
||||
import type { {{^useRxJS}}Promise{{/useRxJS}}HttpLibrary, HttpMethod, RequestContext, ResponseContext } from "../http/http";
|
||||
import type { {{^useRxJS}}Promise{{/useRxJS}}Middleware } from "../middleware";
|
||||
import type { BaseServerConfiguration } from "../servers";
|
||||
|
||||
export abstract class AbstractHttpLibrary implements {{^useRxJS}}Promise{{/useRxJS}}HttpLibrary {
|
||||
public abstract send(request: RequestContext): {{#useRxJS}}Observable{{/useRxJS}}{{^useRxJS}}Promise{{/useRxJS}}<ResponseContext>;
|
||||
};
|
||||
|
||||
export abstract class AbstractMiddleware implements {{^useRxJS}}Promise{{/useRxJS}}Middleware {
|
||||
public abstract pre(context: RequestContext): {{#useRxJS}}Observable{{/useRxJS}}{{^useRxJS}}Promise{{/useRxJS}}<RequestContext>;
|
||||
public abstract post(context: ResponseContext): {{#useRxJS}}Observable{{/useRxJS}}{{^useRxJS}}Promise{{/useRxJS}}<ResponseContext>;
|
||||
}
|
||||
|
||||
export abstract class AbstractServerConfiguration implements BaseServerConfiguration {
|
||||
public abstract makeRequestContext(endpoint: string, httpMethod: HttpMethod): RequestContext;
|
||||
};
|
||||
165
templates/services/index.mustache
Normal file
165
templates/services/index.mustache
Normal file
@@ -0,0 +1,165 @@
|
||||
import { inject, injectable, multiInject, optional, interfaces } from "inversify";
|
||||
|
||||
import { Configuration } from "../configuration";
|
||||
import { ServerConfiguration, servers } from "../servers";
|
||||
import { HttpLibrary{{^useRxJS}}, wrapHttpLibrary{{/useRxJS}} } from "../http/http";
|
||||
import { Middleware{{^useRxJS}}, PromiseMiddlewareWrapper{{/useRxJS}} } from "../middleware";
|
||||
import { authMethodServices, AuthMethods } from "../auth/auth";
|
||||
|
||||
{{#frameworks}}
|
||||
{{#fetch-api}}
|
||||
import { IsomorphicFetchHttpLibrary as DefaultHttpLibrary } from "../http/isomorphic-fetch";
|
||||
{{/fetch-api}}
|
||||
{{#jquery}}
|
||||
import { JQueryHttpLibrary as DefaultHttpLibrary } from "../http/jquery";
|
||||
{{/jquery}}
|
||||
{{/frameworks}}
|
||||
|
||||
import { AbstractHttpLibrary, AbstractMiddleware, AbstractServerConfiguration } from "./http";
|
||||
import { AbstractConfiguration, AbstractAuthMethod, AbstractTokenProvider } from "./configuration";
|
||||
|
||||
export { AbstractHttpLibrary, AbstractMiddleware, AbstractServerConfiguration, AbstractConfiguration, AbstractAuthMethod, AbstractTokenProvider };
|
||||
|
||||
{{#useObjectParameters}}
|
||||
import * as apis from "../types/ObjectParamAPI";
|
||||
import * as apiServices from "./ObjectParamAPI";
|
||||
{{/useObjectParameters}}
|
||||
{{^useObjectParameters}}
|
||||
{{#useRxJS}}
|
||||
import * as apis from "../types/ObservableAPI";
|
||||
import * as apiServices from "./ObservableAPI";
|
||||
{{/useRxJS}}
|
||||
{{^useRxJS}}
|
||||
import * as apis from "../types/PromiseAPI";
|
||||
import * as apiServices from "./PromiseAPI";
|
||||
{{/useRxJS}}
|
||||
{{/useObjectParameters}}
|
||||
|
||||
@injectable()
|
||||
class InjectableConfiguration implements AbstractConfiguration {
|
||||
public httpApi: HttpLibrary = new DefaultHttpLibrary();
|
||||
public middleware: Middleware[] = [];
|
||||
public authMethods: AuthMethods = {};
|
||||
|
||||
constructor(
|
||||
@inject(AbstractServerConfiguration) @optional() public baseServer: AbstractServerConfiguration = servers[0],
|
||||
@inject(AbstractHttpLibrary) @optional() httpApi: AbstractHttpLibrary,
|
||||
@multiInject(AbstractMiddleware) @optional() middleware: AbstractMiddleware[] = [],
|
||||
@multiInject(AbstractAuthMethod) @optional() securityConfiguration: AbstractAuthMethod[] = []
|
||||
) {
|
||||
{{#useRxJS}}
|
||||
this.httpApi = httpApi || new DefaultHttpLibrary();
|
||||
this.middleware = middleware;
|
||||
{{/useRxJS}}
|
||||
{{^useRxJS}}
|
||||
this.httpApi = httpApi === undefined ? new DefaultHttpLibrary() : wrapHttpLibrary(httpApi);
|
||||
for (const _middleware of middleware) {
|
||||
this.middleware.push(new PromiseMiddlewareWrapper(_middleware));
|
||||
}
|
||||
{{/useRxJS}}
|
||||
for (const authMethod of securityConfiguration) {
|
||||
const authName = authMethod.getName();
|
||||
// @ts-ignore
|
||||
if (authMethodServices[authName] !== undefined) {
|
||||
// @ts-ignore
|
||||
this.authMethods[authName] = authMethod;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper class to simplify binding the services
|
||||
*/
|
||||
export class ApiServiceBinder {
|
||||
constructor(private container: interfaces.Container) {
|
||||
this.container.bind(AbstractConfiguration).to(InjectableConfiguration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows you to bind a server configuration without having to import the service identifier.
|
||||
*/
|
||||
public get bindServerConfiguration() {
|
||||
return this.container.bind(AbstractServerConfiguration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use one of the predefined server configurations.
|
||||
*
|
||||
* To customize the server variables you can call `setVariables` on the
|
||||
* return value;
|
||||
*/
|
||||
public bindServerConfigurationToPredefined(idx: number) {
|
||||
this.bindServerConfiguration.toConstantValue(servers[idx]);
|
||||
return servers[idx];
|
||||
}
|
||||
|
||||
/**
|
||||
* Explicitly define the service base url
|
||||
*/
|
||||
public bindServerConfigurationToURL(url: string) {
|
||||
return this.bindServerConfiguration.toConstantValue(
|
||||
new ServerConfiguration<{}>(url, {})
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows you to bind a http library without having to import the service identifier.
|
||||
*/
|
||||
public get bindHttpLibrary() {
|
||||
return this.container.bind(AbstractHttpLibrary);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows you to bind a middleware without having to import the service identifier.
|
||||
*
|
||||
* You can bind multiple middlewares by calling this multiple method times.
|
||||
*/
|
||||
public get bindMiddleware() {
|
||||
return this.container.bind(AbstractMiddleware);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows you to bind an auth method without having to import the service identifier.
|
||||
*
|
||||
* Note: The name of the bound auth method needs to be known in the specs,
|
||||
* because the name is used to decide for which endpoints to apply the authentication.
|
||||
*/
|
||||
public get bindAuthMethod() {
|
||||
return this.container.bind(AbstractAuthMethod);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use one of the predefined auth methods.
|
||||
*
|
||||
* Make sure that you have injected all dependencies for it.
|
||||
*/
|
||||
public bindAuthMethodToPredefined(name: keyof AuthMethods) {
|
||||
return this.bindAuthMethod.to(authMethodServices[name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bind all the apis to their respective service identifiers
|
||||
*
|
||||
* If you want to only bind some of the apis, you need to do that manually.
|
||||
*/
|
||||
public bindAllApiServices() {
|
||||
{{#apiInfo}}
|
||||
{{#apis}}
|
||||
{{#operations}}
|
||||
{{#useObjectParameters}}
|
||||
this.container.bind(apiServices.AbstractObject{{classname}}).to(apis.Object{{classname}}).inSingletonScope();
|
||||
{{/useObjectParameters}}
|
||||
{{^useObjectParameters}}
|
||||
{{#useRxJS}}
|
||||
this.container.bind(apiServices.AbstractObservable{{classname}}).to(apis.Observable{{classname}}).inSingletonScope();
|
||||
{{/useRxJS}}
|
||||
{{^useRxJS}}
|
||||
this.container.bind(apiServices.AbstractPromise{{classname}}).to(apis.Promise{{classname}}).inSingletonScope();
|
||||
{{/useRxJS}}
|
||||
{{/useObjectParameters}}
|
||||
{{/operations}}
|
||||
{{/apis}}
|
||||
{{/apiInfo}}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user