transit-kit 0.4.4 → 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.
@@ -20,11 +20,11 @@ describe("translateToOpenAPIPathItem", () => {
20
20
  dataSchema: z.string(),
21
21
  },
22
22
  },
23
- }, async (req) => {
23
+ }, async ({ request }) => {
24
24
  return {
25
25
  code: 200,
26
26
  dataType: "application/json",
27
- json: req.params.id,
27
+ json: request.params.id,
28
28
  };
29
29
  });
30
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> = (request: Request<PathParams, unknown, RequestBody, Query, Record<string, unknown>>, extractedRequestData: {
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;
@@ -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 caller = response.locals.caller;
12
- const extractedRequestData = {
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
  }
@@ -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,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
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "transit-kit",
3
- "version": "0.4.4",
3
+ "version": "0.5.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",