Skip to content

Commit

Permalink
improve core types, supported interface extension
Browse files Browse the repository at this point in the history
  • Loading branch information
inkognitro committed Apr 22, 2024
1 parent 3643fce commit acad80f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
21 changes: 12 additions & 9 deletions src/templates/ts/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,38 @@ export type EndpointId = {
path: string;
};

type RequestHeaders = {
contentType: string;
[key: string]: string;
};

export type Request<
QueryParams extends object = any,
Body extends object = any,
> = {
endpointId: EndpointId;
contentType: string;
url: string;
supportedSecuritySchemes: string[];
securityScheme: null | string;
headers: object;
appliedSecurityScheme: null | string;
headers: RequestHeaders;
queryParams: QueryParams;
body: Body;
};

type RequestCreationSettings = {
endpointId: EndpointId;
contentType: string;
supportedSecuritySchemes?: [];
urlParams?: UrlParameters;
headers?: {};
headers: RequestHeaders;
queryParams?: object;
body?: object;
};

export function createRequest(settings: RequestCreationSettings): Request {
return {
endpointId: settings.endpointId,
contentType: settings.contentType,
supportedSecuritySchemes: settings.supportedSecuritySchemes ?? [],
securityScheme: null,
appliedSecurityScheme: null,
url: createRequestUrl(settings.endpointId.path, settings.urlParams ?? {}),
headers: settings.headers ?? {},
queryParams: settings.queryParams ?? {},
Expand Down Expand Up @@ -103,9 +105,10 @@ export type RequestResult<
hasRequestBeenCancelled: boolean;
};

export type RequestExecutionConfig = {
export interface RequestExecutionConfig {
onUploadProgress?: () => void;
};
doNotApplyAuthCredentials?: boolean;
}

export interface RequestHandler {
execute(
Expand Down
42 changes: 31 additions & 11 deletions src/templates/ts/core/security.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import {Request} from './core';
import {
Request,
RequestExecutionConfig,
RequestHandler,
RequestResult,
} from './core';

export enum SecuritySchemeType {
httpBearer = 'httpBearer',
Expand Down Expand Up @@ -40,23 +45,38 @@ export type SecurityScheme =
| HttpBearerSecurityScheme
| CustomSecurityScheme;

export class SecuritySchemesRequestModifier {
private readonly securitySchemes: SecurityScheme[];
export class AuthRequestHandler implements RequestHandler {
private readonly supportedSecuritySchemes: SecurityScheme[];
private readonly nextRequestHandler: RequestHandler;

constructor(securitySchemes: SecurityScheme[]) {
this.securitySchemes = securitySchemes;
constructor(
supportedSecuritySchemes: SecurityScheme[],
nextRequestHandler: RequestHandler
) {
this.supportedSecuritySchemes = supportedSecuritySchemes;
this.nextRequestHandler = nextRequestHandler;
}

private getWithSecurityExtendedRequest(request: Request): Request {
execute(
request: Request,
config?: RequestExecutionConfig
): Promise<RequestResult> {
if (config?.doNotApplyAuthCredentials) {
return this.nextRequestHandler.execute(request, config);
}
return this.nextRequestHandler.execute(
this.createRequestWithAuthCredentials(request),
config
);
}

private createRequestWithAuthCredentials(request: Request): Request {
for (const index in request.supportedSecuritySchemes) {
const securitySchemeName = request.supportedSecuritySchemes[index];
const securityScheme = this.securitySchemes.find(
const securityScheme = this.supportedSecuritySchemes.find(
s => s.name === securitySchemeName
);
if (!securityScheme) {
console.warn(
`security scheme with name "${securitySchemeName}" is ignored because it was not found`
);
continue;
}
switch (securityScheme.type) {
Expand Down Expand Up @@ -97,7 +117,7 @@ export class SecuritySchemesRequestModifier {
if (result.securityWasAppliedToRequest) {
return {
...result.request,
securityScheme: securitySchemeName,
appliedSecurityScheme: securitySchemeName,
};
}
break;
Expand Down

0 comments on commit acad80f

Please sign in to comment.