transit-kit 0.7.1 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,6 +8,10 @@ declare function translateToOpenAPIPathItem(definition: ApiEndpointDefinition<st
8
8
  interface GeneratorOptions {
9
9
  title: string;
10
10
  version: string;
11
+ description?: string;
12
+ servers?: OpenAPIV3.ServerObject[];
13
+ contact?: OpenAPIV3.ContactObject;
14
+ license?: OpenAPIV3.LicenseObject;
11
15
  }
12
16
  export declare function generateOpenApiDoc(server: Server, options: GeneratorOptions): Promise<OpenAPIV3.Document>;
13
17
  export declare const __TEST_EXPORTS: {
@@ -155,7 +155,11 @@ export async function generateOpenApiDoc(server, options) {
155
155
  info: {
156
156
  title: options.title,
157
157
  version: options.version,
158
+ description: options.description,
159
+ license: options.license,
160
+ contact: options.contact,
158
161
  },
162
+ servers: options.servers ?? [],
159
163
  paths: paths,
160
164
  components: {
161
165
  securitySchemes,
@@ -1,3 +1,5 @@
1
+ export { HttpStatusCodes, type HttpStatusCode, } from "./constants/HttpStatusCodes.js";
2
+ export { HttpMethods, type HttpMethod } from "./constants/HttpMethods.js";
1
3
  export { createApiEndpointHandler } from "./handlers/api/createApiHandler.js";
2
4
  export { createBasicAuthSchema } from "./security/basicAuth.js";
3
5
  export { createBearerAuthSchema } from "./security/bearerAuth.js";
@@ -1,3 +1,5 @@
1
+ export { HttpStatusCodes, } from "./constants/HttpStatusCodes.js";
2
+ export { HttpMethods } from "./constants/HttpMethods.js";
1
3
  export { createApiEndpointHandler } from "./handlers/api/createApiHandler.js";
2
4
  export { createBasicAuthSchema } from "./security/basicAuth.js";
3
5
  export { createBearerAuthSchema } from "./security/bearerAuth.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "transit-kit",
3
- "version": "0.7.1",
3
+ "version": "0.8.0",
4
4
  "description": "A declarative TypeScript framework for building type-safe Express.js APIs with automatic OpenAPI generation",
5
5
  "keywords": [
6
6
  "express",
@@ -41,7 +41,7 @@
41
41
  "scripts": {
42
42
  "lint": "eslint",
43
43
  "test": "vitest",
44
- "build": "rm -rf ./dist && tsc"
44
+ "build": "rm -rf ./dist && tsc -p ./tsconfig.build.json"
45
45
  },
46
46
  "devDependencies": {
47
47
  "@types/cookie-parser": "^1.4.9",
@@ -1 +0,0 @@
1
- export {};
@@ -1,66 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import z from "zod";
3
- import { createApiEndpointHandler } from "../server/handlers/api/createApiHandler.js";
4
- import { __TEST_EXPORTS } from "./generateOpenApi.js";
5
- const { translateToOpenAPIPathItem } = __TEST_EXPORTS;
6
- describe("translateToOpenAPIPathItem", () => {
7
- it("should translate endpoint definitions to OpenAPI path items", () => {
8
- const { definition } = createApiEndpointHandler({
9
- method: "get",
10
- path: "/users/:id",
11
- meta: {
12
- name: "getUser",
13
- description: "Retrieve a user by ID",
14
- group: "User",
15
- },
16
- securitySchemes: [],
17
- responseSchemas: {
18
- 200: {
19
- type: "json",
20
- schema: z.string(),
21
- },
22
- },
23
- }, async ({ request }) => {
24
- return {
25
- code: 200,
26
- data: request.params.id,
27
- };
28
- });
29
- const [path, pathItem] = translateToOpenAPIPathItem(definition);
30
- const expectedPath = "/users/{id}";
31
- const expectedSchema = {
32
- $schema: "https://json-schema.org/draft/2020-12/schema",
33
- type: "string",
34
- };
35
- const expectedPathItem = {
36
- get: {
37
- operationId: "getUser",
38
- summary: "Retrieve a user by ID",
39
- tags: ["User"],
40
- description: "Retrieve a user by ID",
41
- parameters: [
42
- {
43
- description: "Path parameter :id",
44
- name: "id",
45
- in: "path",
46
- required: true,
47
- schema: { type: "string" },
48
- },
49
- ],
50
- security: [],
51
- responses: {
52
- "200": {
53
- description: "Response for status code 200",
54
- content: {
55
- "application/json": {
56
- schema: expectedSchema,
57
- },
58
- },
59
- },
60
- },
61
- },
62
- };
63
- expect(path).toBe(expectedPath);
64
- expect(pathItem).toEqual(expectedPathItem);
65
- });
66
- });
@@ -1,15 +0,0 @@
1
- import { describe, expectTypeOf, it } from "vitest";
2
- describe("HandlerFromDefinition", () => {
3
- it("can infer handler responses correctly (Json)", () => {
4
- expectTypeOf().toEqualTypeOf();
5
- });
6
- it("can infer handler responses correctly (headers)", () => {
7
- expectTypeOf().toEqualTypeOf();
8
- });
9
- it("can infer caller from auth schema (Basic)", () => {
10
- expectTypeOf().toEqualTypeOf();
11
- });
12
- it("can infer caller from auth schema (Bearer)", () => {
13
- expectTypeOf().toEqualTypeOf();
14
- });
15
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,12 +0,0 @@
1
- import { describe, expectTypeOf, it } from "vitest";
2
- describe("ExtractPathParameters", () => {
3
- it("can infer the parameter types of a path", () => {
4
- expectTypeOf().toEqualTypeOf();
5
- });
6
- it("can infer multiple params", () => {
7
- expectTypeOf().toEqualTypeOf();
8
- });
9
- it("can infer correctly if no param is present", () => {
10
- expectTypeOf().toEqualTypeOf();
11
- });
12
- });
@@ -1,17 +0,0 @@
1
- import z from "zod";
2
- export declare const testEndpointBase: {
3
- meta: {
4
- name: string;
5
- description: string;
6
- group: string;
7
- };
8
- method: "get";
9
- path: string;
10
- securitySchemes: never[];
11
- responseSchemas: {
12
- 200: {
13
- type: "json";
14
- schema: z.ZodString;
15
- };
16
- };
17
- };
@@ -1,73 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { createServer } from "../../server.js";
3
- import { createApiEndpointHandler } from "./createApiHandler.js";
4
- import testRequest from "supertest";
5
- import z from "zod";
6
- import { HttpMethods } from "../../constants/HttpMethods.js";
7
- export const testEndpointBase = {
8
- meta: {
9
- name: "getTest",
10
- description: "Gets test element",
11
- group: "test",
12
- },
13
- method: HttpMethods.get,
14
- path: "/test",
15
- securitySchemes: [],
16
- responseSchemas: {
17
- 200: {
18
- type: "json",
19
- schema: z.string(),
20
- },
21
- },
22
- };
23
- describe("createApiHandler", () => {
24
- it("can create an API handler that responds correctly (json)", async () => {
25
- const endpoint = createApiEndpointHandler({
26
- ...testEndpointBase,
27
- }, async () => {
28
- return {
29
- code: 200,
30
- data: "test",
31
- };
32
- });
33
- const server = createServer({
34
- inDevMode: true,
35
- port: 3000,
36
- logger: false,
37
- });
38
- server.registerApiEndpoint(endpoint);
39
- const response = await testRequest(server.expressApp).get("/test");
40
- expect(response.status).toBe(200);
41
- expect(response.text).toBe("test");
42
- });
43
- it("can create an API handler that responds with custom headers", async () => {
44
- const endpoint = createApiEndpointHandler({
45
- ...testEndpointBase,
46
- responseSchemas: {
47
- 200: {
48
- type: "json",
49
- schema: z.string(),
50
- headers: ["X-Custom-Header"],
51
- },
52
- },
53
- }, async () => {
54
- return {
55
- code: 200,
56
- data: "test",
57
- headers: {
58
- "X-Custom-Header": "CustomValue",
59
- },
60
- };
61
- });
62
- const server = createServer({
63
- inDevMode: true,
64
- port: 3000,
65
- logger: false,
66
- });
67
- server.registerApiEndpoint(endpoint);
68
- const response = await testRequest(server.expressApp).get("/test");
69
- expect(response.status).toBe(200);
70
- expect(response.text).toBe("test");
71
- expect(response.headers["x-custom-header"]).toBe("CustomValue");
72
- });
73
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,46 +0,0 @@
1
- import testRequest from "supertest";
2
- import { describe, expect, it } from "vitest";
3
- import { HttpStatusCodes } from "../constants/HttpStatusCodes.js";
4
- import { createApiEndpointHandler } from "../handlers/api/createApiHandler.js";
5
- import { testEndpointBase } from "../handlers/api/createApiHandler.spec.js";
6
- import { createServer } from "../server.js";
7
- import { createBasicAuthSchema } from "./basicAuth.js";
8
- describe("basic auth schema", () => {
9
- const testUsername = "Test";
10
- const testPassword = "TestPW";
11
- const encodedCredentials = Buffer.from(`${testUsername}:${testPassword}`).toString("base64");
12
- const authScheme = createBasicAuthSchema("TestAuth", async (name, password) => {
13
- if (name === testUsername && password === testPassword) {
14
- return {
15
- username: name,
16
- };
17
- }
18
- else {
19
- return null;
20
- }
21
- });
22
- const endpoint = createApiEndpointHandler({
23
- ...testEndpointBase,
24
- securitySchemes: [authScheme],
25
- }, async () => {
26
- return { code: HttpStatusCodes.Ok_200, data: "test" };
27
- });
28
- const server = createServer({
29
- port: 3000,
30
- inDevMode: false,
31
- logger: false,
32
- });
33
- server.registerApiEndpoint(endpoint);
34
- it("accepts valid credentials", async () => {
35
- const response = await testRequest(server.expressApp)
36
- .get("/test")
37
- .set("Authorization", `Basic ${encodedCredentials}`);
38
- expect(response.status).toBe(HttpStatusCodes.Ok_200);
39
- });
40
- it("rejects invalid credentials", async () => {
41
- const response = await testRequest(server.expressApp)
42
- .get("/test")
43
- .set("Authorization", `Basic INVALID:INVALID`);
44
- expect(response.status).toBe(HttpStatusCodes.Unauthorized_401);
45
- });
46
- });