milkio 0.3.0 → 0.4.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/api-test/index.ts +3 -2
- package/defines/define-api-test.ts +10 -5
- package/defines/define-api.ts +1 -2
- package/kernel/execute.ts +55 -8
- package/kernel/milkio.ts +2 -1
- package/package.json +1 -1
- package/types.ts +5 -0
package/api-test/index.ts
CHANGED
|
@@ -42,8 +42,9 @@ export const executeApiTests = async <Path extends Array<keyof (typeof schema)["
|
|
|
42
42
|
...((await apiTestHooks.default.onBefore()) ?? {}),
|
|
43
43
|
log: (...args: Array<unknown>) => console.log(...args),
|
|
44
44
|
// @ts-ignore
|
|
45
|
-
execute: async (
|
|
46
|
-
executeOther: async (path: any,
|
|
45
|
+
execute: async (options?: any) => app.execute((path as any), options),
|
|
46
|
+
executeOther: async (path: any, options?: any) => app.execute((path as any), options),
|
|
47
|
+
executeStream: async (options?: any) => app.executeStream((path as any), options),
|
|
47
48
|
randParams: () => app.randParams(path as any),
|
|
48
49
|
randOtherParams: (path: any) => app.randParams(path),
|
|
49
50
|
reject: (message?: string) => {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Api, ExecuteResult, ExecuteOptions } from "..";
|
|
1
|
+
import type { Api, ExecuteResult, ExecuteOptions, GeneratorGeneric, ExecuteResultSuccess } from "..";
|
|
2
2
|
import type schema from "../../../generated/api-schema";
|
|
3
3
|
import type apiTestHooks from "../../../src/api-test.ts";
|
|
4
4
|
|
|
@@ -13,13 +13,18 @@ export type ApiTestCases<ApiT extends Api> = {
|
|
|
13
13
|
handler: (
|
|
14
14
|
test: {
|
|
15
15
|
log: (...params: Array<unknown>) => void;
|
|
16
|
-
execute: (params: Parameters<ApiT["action"]>[0], headers?: Record<string, string
|
|
16
|
+
execute: (options: { params: Parameters<ApiT["action"]>[0], headers?: Record<string, string> } & ExecuteOptions) => Promise<ExecuteResult<Awaited<ReturnType<ApiT["action"]>>>>;
|
|
17
17
|
executeOther: <Path extends keyof (typeof schema)["apiMethodsTypeSchema"], Result extends Awaited<ReturnType<(typeof schema)["apiMethodsTypeSchema"][Path]["api"]["action"]>>>(
|
|
18
18
|
path: Path,
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
options: {
|
|
20
|
+
params: Parameters<(typeof schema)["apiMethodsTypeSchema"][Path]["api"]["action"]>[0] | string,
|
|
21
|
+
headers?: Record<string, string>
|
|
22
|
+
} & ExecuteOptions,
|
|
22
23
|
) => Promise<ExecuteResult<Result>>;
|
|
24
|
+
executeStream: (options: { params: Parameters<ApiT["action"]>[0], headers?: Record<string, string> } & ExecuteOptions) => Promise<{
|
|
25
|
+
getResult: () => ExecuteResult<undefined>,
|
|
26
|
+
stream: AsyncGenerator<Awaited<ReturnType<ApiT["action"]>>>
|
|
27
|
+
}>;
|
|
23
28
|
randParams: () => Promise<Parameters<ApiT["action"]>[0]>;
|
|
24
29
|
randOtherParams: <Path extends keyof (typeof schema)["apiMethodsTypeSchema"]>(path: Path) => Promise<Parameters<(typeof schema)["apiMethodsTypeSchema"][Path]["api"]["action"]>[0]>;
|
|
25
30
|
reject: (message?: string) => void;
|
package/defines/define-api.ts
CHANGED
|
@@ -5,8 +5,7 @@ export function defineApi<ApiT extends Api>(api: ApiT): ApiT & { isApi: true } {
|
|
|
5
5
|
return { ...api, isApi: true };
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
export type Api
|
|
8
|
+
export type Api = {
|
|
9
9
|
meta: Meta;
|
|
10
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
11
10
|
action: (params: any, context: Context) => Promise<unknown> | unknown;
|
|
12
11
|
};
|
package/kernel/execute.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Context } from "../../../src/context";
|
|
2
2
|
import { failCode } from "../../../src/fail-code";
|
|
3
3
|
import schema from "../../../generated/api-schema";
|
|
4
|
-
import { type ExecuteId, type ExecuteOptions, type ExecuteResult, createUlid, useLogger, runtime, loggerPushTags, headerToPlainObject, loggerSubmit, type ExecuteCoreOptions, TSON, MiddlewareEvent, reject, _validate } from "..";
|
|
4
|
+
import { type ExecuteId, type ExecuteOptions, type ExecuteResult, createUlid, useLogger, runtime, loggerPushTags, headerToPlainObject, loggerSubmit, type ExecuteCoreOptions, TSON, MiddlewareEvent, reject, _validate, ExecuteStreamResult } from "..";
|
|
5
5
|
import { handleCatchError } from "../utils/handle-catch-error";
|
|
6
6
|
|
|
7
7
|
const apis = new Map<string, any>();
|
|
@@ -102,10 +102,12 @@ export async function _call(
|
|
|
102
102
|
|
|
103
103
|
export async function _execute<Path extends keyof (typeof schema)["apiMethodsTypeSchema"], Result extends Awaited<ReturnType<(typeof schema)["apiMethodsTypeSchema"][Path]["api"]["action"]>>>(
|
|
104
104
|
path: Path,
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
105
|
+
options: {
|
|
106
|
+
params: Parameters<(typeof schema)["apiMethodsTypeSchema"][Path]["api"]["action"]>[0] | string,
|
|
107
|
+
headers?: Record<string, string> | Headers,
|
|
108
|
+
} & ExecuteOptions,
|
|
108
109
|
): Promise<ExecuteResult<Result>> {
|
|
110
|
+
if (!options.headers) options.headers = {};
|
|
109
111
|
const executeId = (options?.executeId ?? createUlid()) as ExecuteId;
|
|
110
112
|
const logger = useLogger(executeId);
|
|
111
113
|
runtime.execute.executeIds.add(executeId);
|
|
@@ -113,11 +115,11 @@ export async function _execute<Path extends keyof (typeof schema)["apiMethodsTyp
|
|
|
113
115
|
loggerPushTags(executeId, {
|
|
114
116
|
from: "execute",
|
|
115
117
|
executeId,
|
|
116
|
-
params,
|
|
118
|
+
params: options.params,
|
|
117
119
|
path,
|
|
118
120
|
});
|
|
119
121
|
|
|
120
|
-
const result = await _call("execute", path, params,
|
|
122
|
+
const result = await _call("execute", path, options.params, options.headers, {
|
|
121
123
|
...options,
|
|
122
124
|
executeId,
|
|
123
125
|
logger,
|
|
@@ -135,8 +137,14 @@ export async function _execute<Path extends keyof (typeof schema)["apiMethodsTyp
|
|
|
135
137
|
return result.$result as ExecuteResult<Result>;
|
|
136
138
|
}
|
|
137
139
|
|
|
138
|
-
export async function _executeToJson<Path extends keyof (typeof schema)["apiMethodsTypeSchema"]>(
|
|
139
|
-
|
|
140
|
+
export async function _executeToJson<Path extends keyof (typeof schema)["apiMethodsTypeSchema"]>(
|
|
141
|
+
path: Path,
|
|
142
|
+
options: {
|
|
143
|
+
params: Parameters<(typeof schema)["apiMethodsTypeSchema"][Path]["api"]["action"]>[0] | string,
|
|
144
|
+
headers?: Record<string, string> | Headers,
|
|
145
|
+
} & ExecuteOptions,
|
|
146
|
+
): Promise<string> {
|
|
147
|
+
const resultsRaw = await _execute(path, options);
|
|
140
148
|
let fn: any;
|
|
141
149
|
try {
|
|
142
150
|
fn = await schema.apiValidator.validate[path]();
|
|
@@ -146,3 +154,42 @@ export async function _executeToJson<Path extends keyof (typeof schema)["apiMeth
|
|
|
146
154
|
const results = await fn.validateResults(TSON.encode(resultsRaw));
|
|
147
155
|
return results;
|
|
148
156
|
}
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
export async function _executeStream<Path extends keyof (typeof schema)["apiMethodsTypeSchema"], Result extends Awaited<ReturnType<(typeof schema)["apiMethodsTypeSchema"][Path]["api"]["action"]>>>(
|
|
161
|
+
path: Path,
|
|
162
|
+
options: {
|
|
163
|
+
params: Parameters<(typeof schema)["apiMethodsTypeSchema"][Path]["api"]["action"]>[0] | string,
|
|
164
|
+
headers?: Record<string, string> | Headers,
|
|
165
|
+
} & ExecuteOptions,
|
|
166
|
+
): Promise<ExecuteStreamResult<Path, Result>> {
|
|
167
|
+
if (!options.headers) options.headers = {};
|
|
168
|
+
const executeId = (options?.executeId ?? createUlid()) as ExecuteId;
|
|
169
|
+
const logger = useLogger(executeId);
|
|
170
|
+
runtime.execute.executeIds.add(executeId);
|
|
171
|
+
|
|
172
|
+
loggerPushTags(executeId, {
|
|
173
|
+
from: "execute",
|
|
174
|
+
executeId,
|
|
175
|
+
params: options.params,
|
|
176
|
+
path,
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
const result = await _call("stream", path, options.params, options.headers, {
|
|
180
|
+
...options,
|
|
181
|
+
executeId,
|
|
182
|
+
logger,
|
|
183
|
+
onAfterHeaders: (headers) => {
|
|
184
|
+
loggerPushTags(executeId, {
|
|
185
|
+
headers: headerToPlainObject(headers),
|
|
186
|
+
});
|
|
187
|
+
},
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
loggerPushTags(executeId, { result: result.$result });
|
|
191
|
+
await loggerSubmit(executeId);
|
|
192
|
+
runtime.execute.executeIds.delete(executeId);
|
|
193
|
+
|
|
194
|
+
return { getResult: () => result.$result, stream: (result as any).$generator } as ExecuteStreamResult<Path, Result>;
|
|
195
|
+
}
|
package/kernel/milkio.ts
CHANGED
|
@@ -3,7 +3,7 @@ import schema from "../../../generated/api-schema";
|
|
|
3
3
|
import { runtime } from "..";
|
|
4
4
|
import { createUlid } from "../utils/create-ulid";
|
|
5
5
|
import { _validate } from "./validate";
|
|
6
|
-
import { _execute, _call, _executeToJson } from "./execute";
|
|
6
|
+
import { _execute, _call, _executeToJson, _executeStream } from "./execute";
|
|
7
7
|
|
|
8
8
|
export type MilkioAppOptions = {
|
|
9
9
|
/**
|
|
@@ -44,6 +44,7 @@ export async function createMilkioApp(MilkioAppOptions: MilkioAppOptions = {}) {
|
|
|
44
44
|
randParams: _randParams,
|
|
45
45
|
execute: _execute,
|
|
46
46
|
executeToJson: _executeToJson,
|
|
47
|
+
executeStream: _executeStream,
|
|
47
48
|
_call,
|
|
48
49
|
};
|
|
49
50
|
|
package/package.json
CHANGED
package/types.ts
CHANGED
|
@@ -52,6 +52,11 @@ export type MilkioConfig = {
|
|
|
52
52
|
|
|
53
53
|
export type ExecuteResult<Result> = ExecuteResultSuccess<Result> | ExecuteResultFail;
|
|
54
54
|
|
|
55
|
+
export type ExecuteStreamResult<Path, Result> = {
|
|
56
|
+
getResult: () => ExecuteResultSuccess<Result> | ExecuteResultFail,
|
|
57
|
+
stream: AsyncGenerator<MilkioEvent<Path>>
|
|
58
|
+
};
|
|
59
|
+
|
|
55
60
|
export type ExecuteResultSuccess<Result> = {
|
|
56
61
|
executeId: ExecuteId;
|
|
57
62
|
success: true;
|