-
Notifications
You must be signed in to change notification settings - Fork 3
/
useDefinyRpc.ts
89 lines (88 loc) · 3.11 KB
/
useDefinyRpc.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { handleRequest } from "https://raw.githubusercontent.com/narumincho/definy/3e527ebb2bfb17f91ed44ecf76a48f31148d4cb8/deno-lib/definyRpc/server/definyRpc.ts";
import { requestObjectToSimpleRequest } from "https://raw.githubusercontent.com/narumincho/definy/3e527ebb2bfb17f91ed44ecf76a48f31148d4cb8/deno-lib/simpleRequestResponse/simpleRequest.ts";
import {
simpleResponseToResponse,
} from "https://raw.githubusercontent.com/narumincho/definy/3e527ebb2bfb17f91ed44ecf76a48f31148d4cb8/deno-lib/simpleRequestResponse/simpleResponse.ts";
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import { createApiFunction } from "https://raw.githubusercontent.com/narumincho/definy/3e527ebb2bfb17f91ed44ecf76a48f31148d4cb8/deno-lib/definyRpc/core/apiFunction.ts";
import {
DefinyRpcTypeInfo,
Field,
FunctionNamespace,
Maybe,
Namespace,
Number,
String,
TypeBody,
Unit,
} from "https://raw.githubusercontent.com/narumincho/definy/3e527ebb2bfb17f91ed44ecf76a48f31148d4cb8/deno-lib/definyRpc/core/coreType.ts";
import { Account } from "./generated/api/main.ts";
serve(async (request) => {
const simpleRequest = await requestObjectToSimpleRequest(request);
if (simpleRequest === undefined) {
throw new Error("解釈できないリクエストだった");
}
const simpleResponse = await handleRequest({
all: () => ({
functionsList: [
createApiFunction({
namespace: FunctionNamespace.local(["main"]),
description: "カスタムAPI Function",
name: "hello",
input: Unit.type(),
output: String.type(),
isMutation: false,
needAuthentication: false,
resolve: () => {
return "hi!";
},
}),
createApiFunction({
namespace: FunctionNamespace.local(["main"]),
description: "カスタムAPI Function",
name: "useCustomType",
input: Unit.type(),
output: Account.type(),
isMutation: false,
needAuthentication: false,
resolve: () => {
return Account.from({
name: "ラフィーア",
age: 18,
});
},
}),
],
typeList: [
DefinyRpcTypeInfo.from({
namespace: Namespace.local(["main"]),
name: "Account",
description: "アカウント",
attribute: Maybe.nothing(),
parameter: [],
body: TypeBody.product([
Field.from({
name: "name",
description: "アカウント名",
type: String.type(),
}),
Field.from({
name: "age",
description: "年齢",
type: Number.type(),
}),
]),
}),
],
}),
codeGenOutputFolderPath: new URL(import.meta.resolve("./generated/")),
name: "hansonTest",
originHint: simpleRequest.url.origin,
}, simpleRequest);
if (simpleResponse === undefined) {
return new Response(JSON.stringify({ error: "notFound..." }), {
status: 404,
});
}
return simpleResponseToResponse(simpleResponse);
});