Skip to content

Commit

Permalink
Fix bindoj apidir typescript (#374)
Browse files Browse the repository at this point in the history
* update bindoj_apidir_typescript.ml

* add type parameter to ApiDir(Get|Post)InvpInfo

* update by review
  • Loading branch information
kxc-wraikny authored Sep 1, 2023
1 parent 4655b03 commit 1e95c26
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 56 deletions.
6 changes: 3 additions & 3 deletions example/ex01/generated/ex01_apidir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ export const Ex01InvpInfo = {
name: "get-any-student",
method: "GET",
urlpath: "/student/any-one",
resp_type: undefined as unknown as Student,
responseType: undefined as unknown as Student,
},
"get-student-from-person": {
name: "get-student-from-person",
method: "POST",
urlpath: "/student/from-person",
req_type: undefined as unknown as Person,
resp_type: undefined as unknown as Student | string,
requestType: undefined as unknown as Person,
responseType: undefined as unknown as Student | string,
},
} as const;
export type Ex01InvpInfoMap = bindoj.IsApiDirInfoMap<typeof Ex01InvpInfo>;
Expand Down
32 changes: 16 additions & 16 deletions example/ex02/generated/ex02_apidir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,57 @@ export const Ex02InvpInfo = {
name: "get-products",
method: "POST",
urlpath: "/products/get",
req_type: undefined as unknown as ProductQuery,
resp_type: undefined as unknown as Product[],
requestType: undefined as unknown as ProductQuery,
responseType: undefined as unknown as Product[],
},
"get-orders": {
name: "get-orders",
method: "POST",
urlpath: "/orders/get",
req_type: undefined as unknown as OrderQuery,
resp_type: undefined as unknown as Order[],
requestType: undefined as unknown as OrderQuery,
responseType: undefined as unknown as Order[],
},
"get-product": {
name: "get-product",
method: "POST",
urlpath: "/product/get",
req_type: undefined as unknown as ProductId,
resp_type: undefined as unknown as Product | string,
requestType: undefined as unknown as ProductId,
responseType: undefined as unknown as Product | string,
},
"get-order": {
name: "get-order",
method: "POST",
urlpath: "/order/get",
req_type: undefined as unknown as OrderId,
resp_type: undefined as unknown as Order | string,
requestType: undefined as unknown as OrderId,
responseType: undefined as unknown as Order | string,
},
"register-product": {
name: "register-product",
method: "POST",
urlpath: "/product/register",
req_type: undefined as unknown as ProductDetails,
resp_type: undefined as unknown as ProductId,
requestType: undefined as unknown as ProductDetails,
responseType: undefined as unknown as ProductId,
},
"register-order": {
name: "register-order",
method: "POST",
urlpath: "/order/register",
req_type: undefined as unknown as OrderDetails,
resp_type: undefined as unknown as OrderId | string | string,
requestType: undefined as unknown as OrderDetails,
responseType: undefined as unknown as OrderId | string | string,
},
"update-product-details": {
name: "update-product-details",
method: "POST",
urlpath: "/product/details/update",
req_type: undefined as unknown as { _0: ProductId; _1: ProductDetails },
resp_type: undefined as unknown as OrderId | string,
requestType: undefined as unknown as { _0: ProductId; _1: ProductDetails },
responseType: undefined as unknown as OrderId | string,
},
"update-order-status": {
name: "update-order-status",
method: "POST",
urlpath: "/order/status/update",
req_type: undefined as unknown as { _0: OrderId; _1: "Unpaid" | "Paid" | "Shipped" | "Delivered" | "Canceled" },
resp_type: undefined as unknown as OrderId | string,
requestType: undefined as unknown as { _0: OrderId; _1: "Unpaid" | "Paid" | "Shipped" | "Delivered" | "Canceled" },
responseType: undefined as unknown as OrderId | string,
},
} as const;
export type Ex02InvpInfoMap = bindoj.IsApiDirInfoMap<typeof Ex02InvpInfo>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ export const Sample_apidir_01InvpInfo = {
name: "get-any-student",
method: "GET",
urlpath: "/student/any-one",
resp_type: undefined as unknown as Student,
responseType: undefined as unknown as Student,
},
"get-student-from-person": {
name: "get-student-from-person",
method: "POST",
urlpath: "/student/from-person",
req_type: undefined as unknown as Person,
resp_type: undefined as unknown as Student,
requestType: undefined as unknown as Person,
responseType: undefined as unknown as Student,
},
} as const;
export type Sample_apidir_01InvpInfoMap = bindoj.IsApiDirInfoMap<typeof Sample_apidir_01InvpInfo>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ export const Sample_apidir_01_no_manglingInvpInfo = {
name: "get-any-student",
method: "GET",
urlpath: "/student/any-one",
resp_type: undefined as unknown as Student,
responseType: undefined as unknown as Student,
},
"get-student-from-person": {
name: "get-student-from-person",
method: "POST",
urlpath: "/student/from-person",
req_type: undefined as unknown as person_no_mangling,
resp_type: undefined as unknown as Student,
requestType: undefined as unknown as person_no_mangling,
responseType: undefined as unknown as Student,
},
} as const;
export type Sample_apidir_01_no_manglingInvpInfoMap = bindoj.IsApiDirInfoMap<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ export const Sample_apidir_02InvpInfo = {
name: "get-any-int-list",
method: "GET",
urlpath: "/int-list/any-one",
resp_type: undefined as unknown as IntListObjtuple,
responseType: undefined as unknown as IntListObjtuple,
},
"inc-int-list": {
name: "inc-int-list",
method: "POST",
urlpath: "/int-list/inc",
req_type: undefined as unknown as IntListObjtuple,
resp_type: undefined as unknown as IntListObjtuple,
requestType: undefined as unknown as IntListObjtuple,
responseType: undefined as unknown as IntListObjtuple,
},
"sum-of-int-list": {
name: "sum-of-int-list",
method: "POST",
urlpath: "/int-list/sum",
req_type: undefined as unknown as IntListObjtuple,
resp_type: undefined as unknown as Int,
requestType: undefined as unknown as IntListObjtuple,
responseType: undefined as unknown as Int,
},
} as const;
export type Sample_apidir_02InvpInfoMap = bindoj.IsApiDirInfoMap<typeof Sample_apidir_02InvpInfo>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export const Sample_apidir_03InvpInfo = {
name: "id-of-person",
method: "POST",
urlpath: "/person/id",
req_type: undefined as unknown as Person,
resp_type: undefined as unknown as Int | String,
requestType: undefined as unknown as Person,
responseType: undefined as unknown as Int | String,
},
} as const;
export type Sample_apidir_03InvpInfoMap = bindoj.IsApiDirInfoMap<typeof Sample_apidir_03InvpInfo>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ export const Sample_apidir_04InvpInfo = {
name: "name-of-named_json",
method: "POST",
urlpath: "/named_json/name",
req_type: undefined as unknown as NamedJson,
resp_type: undefined as unknown as String,
requestType: undefined as unknown as NamedJson,
responseType: undefined as unknown as String,
},
"json-of-named_json": {
name: "json-of-named_json",
method: "POST",
urlpath: "/named_json/json",
req_type: undefined as unknown as NamedJson,
resp_type: undefined as unknown as any,
requestType: undefined as unknown as NamedJson,
responseType: undefined as unknown as any,
},
} as const;
export type Sample_apidir_04InvpInfoMap = bindoj.IsApiDirInfoMap<typeof Sample_apidir_04InvpInfo>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ export const Sample_apidir_05InvpInfo = {
name: "int-of-string",
method: "POST",
urlpath: "/option/int-of-string",
req_type: undefined as unknown as String,
resp_type: undefined as unknown as number | null | undefined,
requestType: undefined as unknown as String,
responseType: undefined as unknown as number | null | undefined,
},
"option-of-complex": {
name: "option-of-complex",
method: "POST",
urlpath: "/option/of-complex",
req_type: undefined as unknown as ComplexTypesNotuple,
resp_type: undefined as unknown as number | null | undefined,
requestType: undefined as unknown as ComplexTypesNotuple,
responseType: undefined as unknown as number | null | undefined,
},
} as const;
export type Sample_apidir_05InvpInfoMap = bindoj.IsApiDirInfoMap<typeof Sample_apidir_05InvpInfo>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ export const Sample_apidir_06InvpInfo = {
name: "get-x",
method: "POST",
urlpath: "/xy-opt/x",
req_type: undefined as unknown as XyOpt,
resp_type: undefined as unknown as number | null | undefined,
requestType: undefined as unknown as XyOpt,
responseType: undefined as unknown as number | null | undefined,
},
"get-y": {
name: "get-y",
method: "POST",
urlpath: "/xy-opt/y",
req_type: undefined as unknown as XyOpt,
resp_type: undefined as unknown as number | null | undefined,
requestType: undefined as unknown as XyOpt,
responseType: undefined as unknown as number | null | undefined,
},
} as const;
export type Sample_apidir_06InvpInfoMap = bindoj.IsApiDirInfoMap<typeof Sample_apidir_06InvpInfo>;
Expand Down
4 changes: 2 additions & 2 deletions src/lib_apidir_typescript/bindoj_apidir_typescript.ml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ let gen_raw :
let endp_urlpath = invp.ip_urlpath |> litstr in
let endp_method = litstr @@ match invp.ip_method with
| `get -> "GET" | `post -> "POST" in
let resp_type_entry = "resp_type", typescript_resptypes invp' |> type_designator in
let resp_type_entry = "responseType", typescript_resptypes invp' |> type_designator in
endp_name,
objexpr ([
"name", litstr endp_name;
Expand All @@ -136,7 +136,7 @@ let gen_raw :
]
| `post ->
Fn.flip List.append [
"req_type", typescript_reqtype invp' |> type_designator;
"requestType", typescript_reqtype invp' |> type_designator;
resp_type_entry;
]
))
Expand Down
4 changes: 2 additions & 2 deletions with_js/apidir-typescript-tests/tests/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ export const createMockClient =
const res: { [prop: string]: any } = {};
for (const [k, v] of Object.entries(invpInfo)) {
if (v.method === "GET") {
res[k] = (): Promise<{body: typeof v.resp_type, status_code: number}> => {
res[k] = (): Promise<{ body: typeof v.responseType; status_code: number }> => {
return mockServer.handle_path_json_get(v.urlpath);
};
} else {
res[k] = (reqBody: typeof v): Promise<{body: typeof v.resp_type, status_code: number}> => {
res[k] = (reqBody: typeof v): Promise<{ body: typeof v.responseType; status_code: number }> => {
return mockServer.handle_path_json_post(v.urlpath, reqBody);
};
}
Expand Down
16 changes: 8 additions & 8 deletions with_js/public-packages/runtime/src/apidir-types.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
export type ApiDirGetInvpInfo = {
export type ApiDirGetInvpInfo<Req> = {
name: string;
urlpath: string;
resp_type: unknown;
responseType: Req;
method: "GET";
};
export type ApiDirPostInvpInfo = {
export type ApiDirPostInvpInfo<Req, Resp> = {
name: string;
urlpath: string;
req_type: unknown;
resp_type: unknown;
requestType: Req;
responseType: Resp;
method: "POST";
};

export type ApiDirInfoMap = {
[invp: string]: ApiDirGetInvpInfo | ApiDirPostInvpInfo;
[invp: string]: ApiDirGetInvpInfo<unknown> | ApiDirPostInvpInfo<unknown, unknown>;
};

export type IsApiDirInfoMap<Dir extends ApiDirInfoMap> = Dir;
Expand All @@ -23,9 +23,9 @@ export type ApiDirAllInvps<InfoMap> = keyof InfoMap;
export type ApiDirInvpRequestType<
Dir extends ApiDirInfoMap,
invp extends keyof Dir
> = Dir[invp] extends ApiDirPostInvpInfo ? Dir[invp]["req_type"] : undefined;
> = Dir[invp] extends ApiDirPostInvpInfo<unknown, unknown> ? Dir[invp]["requestType"] : undefined;

export type ApiDirInvpResponseType<Dir extends ApiDirInfoMap, invp extends keyof Dir> = Dir[invp]["resp_type"];
export type ApiDirInvpResponseType<Dir extends ApiDirInfoMap, invp extends keyof Dir> = Dir[invp]["responseType"];

export type ApiDirInvpFunctionType<
Dir extends ApiDirInfoMap,
Expand Down

0 comments on commit 1e95c26

Please sign in to comment.