transit-kit 0.4.3 → 0.5.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.
- package/dist/cli/generateOpenApi.spec.js +3 -2
- package/dist/server/handlers/api/EndpointDefinition.d.ts +1 -1
- package/dist/server/handlers/api/EndpointHandler.d.ts +4 -2
- package/dist/server/handlers/api/HandlerFromDefinition.spec.js +6 -0
- package/dist/server/handlers/api/createApiHandler.d.ts +4 -4
- package/dist/server/handlers/api/createApiHandler.js +5 -5
- package/dist/server/handlers/api/createApiHandler.spec.d.ts +17 -1
- package/dist/server/handlers/api/createApiHandler.spec.js +18 -11
- package/dist/server/handlers/api/responses/emptyResponse.spec.d.ts +1 -0
- package/dist/server/handlers/api/responses/emptyResponse.spec.js +9 -0
- package/dist/server/handlers/api/responses/jsonResponse.spec.d.ts +1 -0
- package/dist/server/handlers/api/responses/jsonResponse.spec.js +39 -0
- package/dist/server/security/basicAuth.spec.d.ts +1 -0
- package/dist/server/security/basicAuth.spec.js +45 -0
- package/dist/server/server.d.ts +1 -1
- package/package.json +1 -1
|
@@ -13,17 +13,18 @@ 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",
|
|
19
20
|
dataSchema: z.string(),
|
|
20
21
|
},
|
|
21
22
|
},
|
|
22
|
-
}, async (
|
|
23
|
+
}, async ({ request }) => {
|
|
23
24
|
return {
|
|
24
25
|
code: 200,
|
|
25
26
|
dataType: "application/json",
|
|
26
|
-
json:
|
|
27
|
+
json: request.params.id,
|
|
27
28
|
};
|
|
28
29
|
});
|
|
29
30
|
const [path, pathItem] = translateToOpenAPIPathItem(definition);
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import { Request } from "express";
|
|
1
|
+
import { Request, Response } from "express";
|
|
2
2
|
import { HttpStatusCodes } from "../../constants/HttpStatusCodes";
|
|
3
3
|
import { GenericResponse } from "./responses";
|
|
4
|
-
export type ApiEndpointHandler<PathParams extends Record<string, string> = {}, RequestBody = unknown, Query = unknown, Responses extends GenericResponse = never, Caller = unknown> = (
|
|
4
|
+
export type ApiEndpointHandler<PathParams extends Record<string, string> = {}, RequestBody = unknown, Query = unknown, Responses extends GenericResponse = never, Caller = unknown> = (typedRequestData: {
|
|
5
|
+
request: Request<PathParams, unknown, RequestBody, Query, Record<string, unknown>>;
|
|
6
|
+
response: Response<unknown>;
|
|
5
7
|
parameters: PathParams;
|
|
6
8
|
query: Query;
|
|
7
9
|
body: RequestBody;
|
|
@@ -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
|
|
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>>;
|
|
@@ -8,14 +8,14 @@ export function createApiEndpointHandler(definition, handler) {
|
|
|
8
8
|
}
|
|
9
9
|
export function buildApiEndpointHandler(handler) {
|
|
10
10
|
return expressAsyncHandler(async (request, response) => {
|
|
11
|
-
const
|
|
12
|
-
|
|
11
|
+
const result = await handler({
|
|
12
|
+
request,
|
|
13
|
+
response,
|
|
13
14
|
parameters: request.params,
|
|
14
15
|
query: request.query,
|
|
15
16
|
body: request.body,
|
|
16
|
-
caller,
|
|
17
|
-
};
|
|
18
|
-
const result = await handler(request, extractedRequestData);
|
|
17
|
+
caller: response.locals.caller,
|
|
18
|
+
});
|
|
19
19
|
if (isJsonResponse(result)) {
|
|
20
20
|
response.status(result.code).json(result.json);
|
|
21
21
|
}
|
|
@@ -1 +1,17 @@
|
|
|
1
|
-
|
|
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
|
-
|
|
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,9 @@
|
|
|
1
|
+
import { describe, expectTypeOf, it } from "vitest";
|
|
2
|
+
describe("empty response", () => {
|
|
3
|
+
it("can correctly infer the response type from schema", () => {
|
|
4
|
+
expectTypeOf().toEqualTypeOf();
|
|
5
|
+
});
|
|
6
|
+
it("can correctly identify a literal with the type", () => {
|
|
7
|
+
expectTypeOf().toEqualTypeOf();
|
|
8
|
+
});
|
|
9
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { describe, expect, expectTypeOf, it } from "vitest";
|
|
2
|
+
import z from "zod";
|
|
3
|
+
import { isJsonResponse, isJsonResponseSchema, } from "./jsonResponse";
|
|
4
|
+
describe("empty response", () => {
|
|
5
|
+
it("can correctly infer the response type from schema", () => {
|
|
6
|
+
expectTypeOf().toEqualTypeOf();
|
|
7
|
+
});
|
|
8
|
+
it("can correctly identify a literal with the type", () => {
|
|
9
|
+
expectTypeOf().toEqualTypeOf();
|
|
10
|
+
});
|
|
11
|
+
it("can correctly validate if a response is of type", () => {
|
|
12
|
+
const res = {
|
|
13
|
+
dataType: "application/json",
|
|
14
|
+
json: "string",
|
|
15
|
+
};
|
|
16
|
+
expect(isJsonResponse(res)).toBe(true);
|
|
17
|
+
});
|
|
18
|
+
it("can correctly validate if a response is not of type", () => {
|
|
19
|
+
const res = {
|
|
20
|
+
dataType: "application/data", //<=== Wrong
|
|
21
|
+
json: "string",
|
|
22
|
+
};
|
|
23
|
+
expect(isJsonResponse(res)).toBe(false);
|
|
24
|
+
});
|
|
25
|
+
it("can correctly validate if a response schema is of type", () => {
|
|
26
|
+
const res = {
|
|
27
|
+
dataType: "application/json",
|
|
28
|
+
dataSchema: z.string(),
|
|
29
|
+
};
|
|
30
|
+
expect(isJsonResponseSchema(res)).toBe(true);
|
|
31
|
+
});
|
|
32
|
+
it("can correctly validate if a response schema is not of type", () => {
|
|
33
|
+
const res = {
|
|
34
|
+
dataType: "application/data", // <=== Wrong
|
|
35
|
+
dataSchema: z.string(),
|
|
36
|
+
};
|
|
37
|
+
expect(isJsonResponseSchema(res)).toBe(false);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
@@ -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
|
+
});
|
package/dist/server/server.d.ts
CHANGED
|
@@ -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
|
}
|