Skip to content

Commit

Permalink
Merge branch 'main' into renovate/typescript-5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
pmcelhaney authored Jul 30, 2024
2 parents aaed78b + d0b9f22 commit 2761345
Show file tree
Hide file tree
Showing 20 changed files with 180 additions and 155 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
fetch-depth: 0
- uses: actions/setup-node@v4
with:
node-version: 22.x
node-version: lts/*
cache: yarn
id: setup-node
- name: Get Node Version
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/coveralls.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ jobs:
steps:
- uses: actions/checkout@v4

- name: Setup Node.js 18.x
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 18.x
node-version: lts/*
cache: yarn

- name: install, run tests
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/debug-windows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
fetch-depth: 0
- uses: actions/setup-node@v4
with:
node-version: 18.x
node-version: lts/*
cache: yarn
id: setup-node
- name: Get Node Version
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/mutation-testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
fetch-depth: 0
- uses: actions/setup-node@v4
with:
node-version: 18.x
node-version: lts/*
cache: yarn
id: setup-node
- name: Get Node Version
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:
- name: Checkout Repo
uses: actions/checkout@v4

- name: Setup Node.js 18.x
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 18.x
node-version: lts/*
cache: yarn

- name: Get Node Version
Expand Down
4 changes: 2 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export default {
global: {
branches: 80,
functions: 80,
lines: 80,
statements: 80,
lines: 77,
statements: 77,
},
},

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"test": "yarn node --experimental-vm-modules ./node_modules/jest-cli/bin/jest --testPathIgnorePatterns=black-box",
"test:black-box": "rimraf dist && rimraf out && yarn build && yarn node --experimental-vm-modules ./node_modules/jest-cli/bin/jest black-box --forceExit --coverage=false",
"test:mutants": "stryker run stryker.config.json",
"build": "tsc && copyfiles -f \"src/client/**\" dist/client && copyfiles -f \"src/server/*.d.ts\" dist/server && copyfiles -f \"src/server/*.cjs\" dist/server",
"build": "tsc && copyfiles -f \"src/client/**\" dist/client && copyfiles -f \"src/server/types.ts\" dist/server && copyfiles -f \"src/server/*.cjs\" dist/server",
"prepack": "yarn build",
"release": "npx changeset publish",
"prepare": "husky install",
Expand Down Expand Up @@ -70,7 +70,7 @@
"eslint-plugin-jest-dom": "5.4.0",
"eslint-plugin-no-explicit-type-exports": "0.12.1",
"eslint-plugin-unused-imports": "4.0.1",
"husky": "9.1.2",
"husky": "9.1.3",
"jest": "29.7.0",
"node-mocks-http": "1.15.0",
"nodemon": "3.1.4",
Expand Down
2 changes: 1 addition & 1 deletion src/server/dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {
} from "./registry.js";
import { createResponseBuilder } from "./response-builder.js";
import { Tools } from "./tools.js";
import type { OpenApiOperation, OpenApiParameters } from "./types.d.ts";
import type { OpenApiOperation, OpenApiParameters } from "./types.ts";

const debug = createDebugger("counterfact:server:dispatcher");

Expand Down
2 changes: 1 addition & 1 deletion src/server/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import createDebugger from "debug";

import { ModuleTree } from "./module-tree.js";
import type { Tools } from "./tools.js";
import type { MediaType, ResponseBuilderFactory } from "./types.d.ts";
import type { MediaType, ResponseBuilderFactory } from "./types.ts";

const debug = createDebugger("counterfact:server:registry");

Expand Down
2 changes: 1 addition & 1 deletion src/server/response-builder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { JSONSchemaFaker } from "json-schema-faker";

import { jsonToXml } from "./json-to-xml.js";
import type { OpenApiOperation, ResponseBuilder } from "./types.d.ts";
import type { OpenApiOperation, ResponseBuilder } from "./types.ts";

JSONSchemaFaker.option("useExamplesValue", true);
JSONSchemaFaker.option("minItems", 0);
Expand Down
23 changes: 18 additions & 5 deletions src/server/types.d.ts → src/server/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable etc/no-t */
interface OpenApiHeader {
schema: unknown;
}
Expand All @@ -12,6 +14,14 @@ interface Example {
value: unknown;
}

const counterfactResponse = Symbol("Counterfact Response");

const counterfactResponseObject = {
[counterfactResponse]: counterfactResponse,
};

type COUNTERFACT_RESPONSE = typeof counterfactResponseObject;

type MediaType = `${string}/${string}`;

type OmitAll<T, K extends (keyof T)[]> = {
Expand Down Expand Up @@ -64,6 +74,7 @@ type MaybeShortcut<
never
>;

// eslint-disable-next-line @typescript-eslint/ban-types
type NeverIfEmpty<Record> = {} extends Record ? never : Record;

type MatchFunction<Response extends OpenApiResponse> = <
Expand All @@ -89,8 +100,9 @@ type HeaderFunction<Response extends OpenApiResponse> = <
}>;

type RandomFunction<Response extends OpenApiResponse> = <
// eslint-disable-next-line etc/no-misused-generics, unused-imports/no-unused-vars
Header extends string & keyof Response["headers"],
>() => "COUNTERFACT_RESPONSE";
>() => COUNTERFACT_RESPONSE;

interface ResponseBuilder {
[status: number | `${number} ${string}`]: ResponseBuilder;
Expand All @@ -107,8 +119,6 @@ interface ResponseBuilder {
xml: (body: unknown) => ResponseBuilder;
}

type ArrayToUnion<T extends readonly any[]> = T[number];

type GenericResponseBuilderInner<
Response extends OpenApiResponse = OpenApiResponse,
> = OmitValueWhenNever<{
Expand Down Expand Up @@ -138,11 +148,12 @@ type GenericResponseBuilderInner<

type GenericResponseBuilder<
Response extends OpenApiResponse = OpenApiResponse,
// eslint-disable-next-line @typescript-eslint/ban-types
> = {} extends OmitValueWhenNever<Response>
? "COUNTERFACT_RESPONSE"
? COUNTERFACT_RESPONSE
: keyof OmitValueWhenNever<Response> extends "headers"
? {
ALL_REMAINING_HEADERS_ARE_OPTIONAL: "COUNTERFACT_RESPONSE";
ALL_REMAINING_HEADERS_ARE_OPTIONAL: COUNTERFACT_RESPONSE;
header: HeaderFunction<Response>;
}
: GenericResponseBuilderInner<Response>;
Expand Down Expand Up @@ -258,6 +269,8 @@ interface WideOperationArgument {
response: { [key: number]: WideResponseBuilder };
}

export type { COUNTERFACT_RESPONSE };

export type {
HttpStatusCode,
MediaType,
Expand Down
5 changes: 3 additions & 2 deletions src/typescript-generator/operation-type-coder.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export class OperationTypeCoder extends TypeCoder {
const xType = script.importSharedType("WideOperationArgument");

script.importSharedType("OmitValueWhenNever");
script.importSharedType("COUNTERFACT_RESPONSE");

const contextTypeImportName = script.importExternalType(
"Context",
Expand Down Expand Up @@ -134,10 +135,10 @@ export class OperationTypeCoder extends TypeCoder {
this.requirement.specification?.rootRequirement?.get("produces")?.data,
).write(script);

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

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" }`;
)} | { status: 415, contentType: "text/plain", body: string } | COUNTERFACT_RESPONSE | { ALL_REMAINING_HEADERS_ARE_OPTIONAL: COUNTERFACT_RESPONSE }`;
}
}
4 changes: 2 additions & 2 deletions src/typescript-generator/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ export class Repository {
}

async copyCoreFiles(destination) {
const sourcePath = nodePath.join(__dirname, "../../dist/server/types.d.ts");
const destinationPath = nodePath.join(destination, "types.d.ts");
const sourcePath = nodePath.join(__dirname, "../../dist/server/types.ts");
const destinationPath = nodePath.join(destination, "types.ts");

if (!existsSync(sourcePath)) {
return false;
Expand Down
4 changes: 1 addition & 3 deletions src/typescript-generator/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,7 @@ export class Script {
importSharedType(name) {
return this.importExternal(
name,
nodePath
.join(this.relativePathToBase, "types.d.ts")
.replaceAll("\\", "/"),
nodePath.join(this.relativePathToBase, "types.ts").replaceAll("\\", "/"),
true,
);
}
Expand Down
2 changes: 1 addition & 1 deletion templates/response-builder-factory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { OpenApiResponse } from "../src/server/types.d.ts";
import type { OpenApiResponse } from "../src/server/types.ts";

type OmitValueWhenNever<Base> = Pick<
Base,
Expand Down
2 changes: 1 addition & 1 deletion test/server/response-builder.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createResponseBuilder } from "../../src/server/response-builder.js";
import type { OpenApiOperation } from "../../src/server/types.d.ts";
import type { OpenApiOperation } from "../../src/server/types.ts";

describe("a response builder", () => {
it("starts building a response object when the status is selected", () => {
Expand Down
Loading

0 comments on commit 2761345

Please sign in to comment.