transit-kit 0.4.3 → 0.4.4

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.
@@ -13,6 +13,7 @@ describe("translateToOpenAPIPathItem", () => {
13
13
  description: "Retrieve a user by ID",
14
14
  group: "User",
15
15
  },
16
+ securitySchemes: [],
16
17
  responseSchemas: {
17
18
  200: {
18
19
  dataType: "application/json",
@@ -14,5 +14,5 @@ export type ApiEndpointDefinition<Path extends string = string, Method extends H
14
14
  requestBodySchema?: RequestBody;
15
15
  querySchema?: Query;
16
16
  responseSchemas: ResponseMap;
17
- securitySchemes?: SecuritySchemes;
17
+ securitySchemes: SecuritySchemes;
18
18
  };
@@ -16,4 +16,10 @@ describe("HandlerFromDefinition", () => {
16
16
  });
17
17
  expectTypeOf().toEqualTypeOf();
18
18
  });
19
+ it("can infer caller from auth schema (Basic)", () => {
20
+ expectTypeOf().toEqualTypeOf();
21
+ });
22
+ it("can infer caller from auth schema (Bearer)", () => {
23
+ expectTypeOf().toEqualTypeOf();
24
+ });
19
25
  });
@@ -6,7 +6,7 @@ import { ApiEndpointDefinition } from "./EndpointDefinition";
6
6
  import { ApiEndpointHandler } from "./EndpointHandler";
7
7
  import { HandlerForDefinition } from "./HandlerFromDefinition";
8
8
  import { GenericResponse, GenericResponseSchemaMap } from "./responses";
9
- export declare function createApiEndpointHandler<const ResponsesMap extends GenericResponseSchemaMap, const Path extends string, const Method extends HttpMethod, const RequestBody extends z.ZodType | undefined = undefined, const Query extends z.ZodType | undefined = undefined, const SecuritySchemas extends SecurityScheme<unknown>[] = []>(definition: Prettify<ApiEndpointDefinition<Path, Method, RequestBody, Query, ResponsesMap, SecuritySchemas>>, handler: HandlerForDefinition<Path, RequestBody, Query, ResponsesMap>): {
9
+ export declare function createApiEndpointHandler<const ResponsesMap extends GenericResponseSchemaMap, const Path extends string, const Method extends HttpMethod, const RequestBody extends z.ZodType | undefined = undefined, const Query extends z.ZodType | undefined = undefined, const SecuritySchemas extends SecurityScheme<unknown>[] = []>(definition: Prettify<ApiEndpointDefinition<Path, Method, RequestBody, Query, ResponsesMap, SecuritySchemas>>, handler: HandlerForDefinition<Path, RequestBody, Query, ResponsesMap, SecuritySchemas>): {
10
10
  definition: {
11
11
  meta: import("./EndpointDefinition").ApiEndpointMeta;
12
12
  path: Path;
@@ -14,8 +14,8 @@ export declare function createApiEndpointHandler<const ResponsesMap extends Gene
14
14
  requestBodySchema?: RequestBody | undefined;
15
15
  querySchema?: Query | undefined;
16
16
  responseSchemas: ResponsesMap;
17
- securitySchemes?: SecuritySchemas | undefined;
17
+ securitySchemes: SecuritySchemas;
18
18
  };
19
- handler: HandlerForDefinition<Path, RequestBody, Query, ResponsesMap>;
19
+ handler: HandlerForDefinition<Path, RequestBody, Query, ResponsesMap, SecuritySchemas>;
20
20
  };
21
- export declare function buildApiEndpointHandler<Handler extends ApiEndpointHandler<Record<string, string>, unknown, unknown, GenericResponse>>(handler: Handler): import("express").RequestHandler<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>;
21
+ export declare function buildApiEndpointHandler<Handler extends ApiEndpointHandler<Record<string, string>, unknown, unknown, GenericResponse, unknown>>(handler: Handler): import("express").RequestHandler<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>;
@@ -1 +1,17 @@
1
- export {};
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
+ requestBodySchema: z.ZodObject<{
10
+ name: z.ZodString;
11
+ }, z.core.$strip>;
12
+ path: string;
13
+ securitySchemes: never[];
14
+ responseSchemas: {
15
+ 200: {};
16
+ };
17
+ };
@@ -3,20 +3,27 @@ import { createServer } from "../../server";
3
3
  import { createApiEndpointHandler } from "./createApiHandler";
4
4
  import testRequest from "supertest";
5
5
  import z from "zod";
6
+ import { HttpMethods } from "../../constants/HttpMethods";
7
+ export const testEndpointBase = {
8
+ meta: {
9
+ name: "getTest",
10
+ description: "Gets test element",
11
+ group: "test",
12
+ },
13
+ method: HttpMethods.get,
14
+ requestBodySchema: z.object({
15
+ name: z.string(),
16
+ }),
17
+ path: "/test",
18
+ securitySchemes: [],
19
+ responseSchemas: {
20
+ 200: {},
21
+ },
22
+ };
6
23
  describe("createApiHandler", () => {
7
24
  it("can create an API handler", () => {
8
25
  const endpoint = createApiEndpointHandler({
9
- meta: {
10
- name: "",
11
- description: "",
12
- group: "",
13
- },
14
- method: "get",
15
- path: "/test",
16
- requestBodySchema: z.string(),
17
- responseSchemas: {
18
- 200: {},
19
- },
26
+ ...testEndpointBase,
20
27
  }, async () => {
21
28
  return {
22
29
  code: 200,
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,45 @@
1
+ import testRequest from "supertest";
2
+ import { describe, it } from "vitest";
3
+ import { HttpStatusCodes } from "../constants/HttpStatusCodes";
4
+ import { createApiEndpointHandler } from "../handlers/api/createApiHandler";
5
+ import { testEndpointBase } from "../handlers/api/createApiHandler.spec";
6
+ import { createServer } from "../server";
7
+ import { createBasicAuthSchema } from "./basicAuth";
8
+ describe("basic auth schema", () => {
9
+ const testUsername = "Test";
10
+ const testPassword = "TestPW";
11
+ const authScheme = createBasicAuthSchema("TestAuth", async (name, password) => {
12
+ if (name === testUsername && password === testPassword) {
13
+ return {
14
+ username: name,
15
+ };
16
+ }
17
+ else {
18
+ return null;
19
+ }
20
+ });
21
+ const endpoint = createApiEndpointHandler({
22
+ ...testEndpointBase,
23
+ securitySchemes: [authScheme],
24
+ }, async () => {
25
+ return { code: HttpStatusCodes.Ok_200 };
26
+ });
27
+ const server = createServer({
28
+ port: 3000,
29
+ inDevMode: false,
30
+ logger: false,
31
+ });
32
+ server.registerApiEndpoint(endpoint);
33
+ it("accepts valid credentials", () => {
34
+ testRequest(server.expressApp)
35
+ .get("/test")
36
+ .set("Authorization", `Basic ${testUsername}:${testPassword}`)
37
+ .expect(HttpStatusCodes.Ok_200);
38
+ });
39
+ it("rejectes invalid credentials", () => {
40
+ testRequest(server.expressApp)
41
+ .get("/test")
42
+ .set("Authorization", `Basic invalid:invalid`)
43
+ .expect(HttpStatusCodes.Unauthorized_401);
44
+ });
45
+ });
@@ -13,7 +13,7 @@ export interface Server {
13
13
  endpointDefinitions: ApiEndpointDefinition[];
14
14
  registerApiEndpoint<Definition extends ApiEndpointDefinition>({ definition, handler, }: {
15
15
  definition: Definition;
16
- handler: HandlerForDefinition<Definition["path"], Definition["requestBodySchema"], Definition["querySchema"], Definition["responseSchemas"]>;
16
+ handler: HandlerForDefinition<Definition["path"], Definition["requestBodySchema"], Definition["querySchema"], Definition["responseSchemas"], Definition["securitySchemes"]>;
17
17
  }): void;
18
18
  start: () => void;
19
19
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "transit-kit",
3
- "version": "0.4.3",
3
+ "version": "0.4.4",
4
4
  "description": "A declarative TypeScript framework for building type-safe Express.js APIs with automatic OpenAPI generation",
5
5
  "keywords": [
6
6
  "express",