Skip to content

Commit

Permalink
typescript generator for security object => $.user
Browse files Browse the repository at this point in the history
  • Loading branch information
pmcelhaney committed Apr 17, 2024
1 parent 6ff2dc9 commit 565db42
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 58 deletions.
3 changes: 3 additions & 0 deletions openapi-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ info:
version: 1.0.0
title: Sample API
description: A sample API to illustrate OpenAPI concepts
security:
- type: ["http"]
scheme: ["basic"]
paths:
/count:
get:
Expand Down
7 changes: 6 additions & 1 deletion src/typescript-generator/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,17 @@ export async function generate(

debug("got %i paths", paths.size);

const securityRequirement = await specification.requirementAt("#/security");

const security = securityRequirement?.data ?? [];

paths.forEach((pathDefinition, key) => {
debug("processing path %s", key);

pathDefinition.forEach((operation, requestMethod) => {
repository
.get(`paths${key}.ts`)
.export(new OperationCoder(operation, requestMethod));
.export(new OperationCoder(operation, requestMethod, security));
});
});

Expand Down
4 changes: 3 additions & 1 deletion src/typescript-generator/operation-coder.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import { Coder } from "./coder.js";
import { OperationTypeCoder } from "./operation-type-coder.js";

export class OperationCoder extends Coder {
constructor(requirement, requestMethod) {
constructor(requirement, requestMethod, security = []) {
super(requirement);

if (requestMethod === undefined) {
throw new Error("requestMethod is required");
}

this.requestMethod = requestMethod;
this.security = security;
}

names() {
Expand Down Expand Up @@ -46,6 +47,7 @@ export class OperationCoder extends Coder {
const operationTypeCoder = new OperationTypeCoder(
this.requirement,
this.requestMethod,
this.security,
);

return script.importType(operationTypeCoder);
Expand Down
17 changes: 15 additions & 2 deletions src/typescript-generator/operation-type-coder.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import { SchemaTypeCoder } from "./schema-type-coder.js";
import { TypeCoder } from "./type-coder.js";

export class OperationTypeCoder extends TypeCoder {
constructor(requirement, requestMethod) {
constructor(requirement, requestMethod, security = []) {
super(requirement);

if (requestMethod === undefined) {
throw new Error("requestMethod is required");
}

this.requestMethod = requestMethod;
this.security = security;
}

names() {
Expand Down Expand Up @@ -76,6 +77,18 @@ export class OperationTypeCoder extends TypeCoder {
.replaceAll("\\", "/")}.types.ts`;
}

userType() {
if (
this.security.some(
({ scheme, type }) => type.includes("http") && scheme.includes("basic"),
)
) {
return "{username?: string, password?: string}";
}

return "never";
}

// eslint-disable-next-line max-statements
writeCode(script) {
// eslint-disable-next-line no-param-reassign
Expand Down Expand Up @@ -123,7 +136,7 @@ export class OperationTypeCoder extends TypeCoder {

const proxyType = '(url: string) => "COUNTERFACT_RESPONSE"';

return `($: OmitValueWhenNever<{ query: ${queryType}, path: ${pathType}, header: ${headerType}, body: ${bodyType}, context: ${contextTypeImportName}, response: ${responseType}, x: ${xType}, proxy: ${proxyType} }>) => ${this.responseTypes(
return `($: OmitValueWhenNever<{ query: ${queryType}, path: ${pathType}, header: ${headerType}, body: ${bodyType}, context: ${contextTypeImportName}, response: ${responseType}, x: ${xType}, proxy: ${proxyType}, user: ${this.userType()} }>) => ${this.responseTypes(
script,
)} | { status: 415, contentType: "text/plain", body: string } | "COUNTERFACT_RESPONSE" | { ALL_REMAINING_HEADERS_ARE_OPTIONAL: "COUNTERFACT_RESPONSE" }`;
}
Expand Down

0 comments on commit 565db42

Please sign in to comment.