milkio 0.0.13 → 0.0.15
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 +5 -1
- package/c.ts +1 -0
- package/defines/define-api-test.ts +4 -3
- package/kernel/milkio.ts +11 -5
- package/kernel/validate.ts +4 -2
- package/package.json +1 -1
- package/scripts/gen-insignificant.ts +6 -6
- package/scripts/gen-significant.ts +9 -9
- package/templates/api.ts +11 -15
- package/utils/create-template.ts +10 -0
package/api-test/index.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
/* eslint-disable no-constant-condition, @typescript-eslint/no-unsafe-argument, no-console, @typescript-eslint/no-explicit-any */
|
|
2
2
|
|
|
3
3
|
import schema from "../../../generated/api-schema"
|
|
4
|
-
import { type MilkioApp } from ".."
|
|
4
|
+
import { useLogger, type MilkioApp } from ".."
|
|
5
5
|
|
|
6
6
|
export const executeApiTests = async <Path extends Array<keyof (typeof schema)["apiTestsSchema"]>>(app: MilkioApp, path: Path | string | true | 1 | undefined) => {
|
|
7
7
|
console.log(`🥛 Milkio Api Testing..\n`)
|
|
8
8
|
|
|
9
|
+
const logger = useLogger('global')
|
|
9
10
|
let pathArr = [] as Array<string>
|
|
10
11
|
if (!path || path === "1" || path === 1 || path === true) {
|
|
11
12
|
pathArr = Object.keys(schema.apiTestsSchema) as unknown as Path
|
|
@@ -37,9 +38,12 @@ export const executeApiTests = async <Path extends Array<keyof (typeof schema)["
|
|
|
37
38
|
throw new Error("")
|
|
38
39
|
}, cs.timeout ?? 8192)
|
|
39
40
|
await cs.handler({
|
|
41
|
+
log: (...args: Array<unknown>) => console.log(...args),
|
|
40
42
|
// @ts-ignore
|
|
41
43
|
execute: async (params: any, headers?: any, options?: any) => app.execute(path, params, headers ?? {}, options),
|
|
42
44
|
executeOther: async (path: any, params: any, headers?: any, options?: any) => app.execute(path, params, headers ?? {}, options),
|
|
45
|
+
randParams: () => app.randParams(path as any),
|
|
46
|
+
randOtherParams: (path: any) => app.randParams(path),
|
|
43
47
|
reject: (message?: string) => {
|
|
44
48
|
console.error(`------`)
|
|
45
49
|
console.error(`❌ REJECT -- ${message ?? "Test not satisfied"}`)
|
package/c.ts
CHANGED
|
@@ -29,6 +29,7 @@ const commands = {
|
|
|
29
29
|
async EAR(commandBase64ed: string) {
|
|
30
30
|
try { await $`clear` } catch (e) {}
|
|
31
31
|
const command = Buffer.from(commandBase64ed, 'base64').toString('utf-8')
|
|
32
|
+
console.log('\x1B[2m%s\x1B[0m', `$ ${command}`)
|
|
32
33
|
await $`${{ raw: command }}`
|
|
33
34
|
process.on('SIGINT', () => {}) // prevent users from exiting by pressing ctrl + c
|
|
34
35
|
while (true) await new Promise((resolve) => process.stdin.on('keypress', resolve))
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Logger } from ".."
|
|
1
2
|
import { type Api, type ExecuteResult, type ExecuteOptions } from ".."
|
|
2
3
|
import type schema from "../../../generated/api-schema"
|
|
3
4
|
|
|
@@ -10,11 +11,11 @@ export function defineApiTest<ApiT extends Api>(_api: ApiT, cases: Array<ApiTest
|
|
|
10
11
|
|
|
11
12
|
export type ApiTestCases<ApiT extends Api> = {
|
|
12
13
|
handler: (test: {
|
|
13
|
-
|
|
14
|
+
log: (...params: Array<unknown>) => void;
|
|
14
15
|
execute: (params: Parameters<ApiT["action"]>[0], headers?: Record<string, string>, options?: ExecuteOptions) => Promise<ExecuteResult<Awaited<ReturnType<ApiT["action"]>>>>;
|
|
15
|
-
// execute other
|
|
16
16
|
executeOther: <Path extends keyof (typeof schema)["apiMethodsTypeSchema"], Result extends Awaited<ReturnType<(typeof schema)["apiMethodsTypeSchema"][Path]["api"]["action"]>>>(path: Path, params: Parameters<(typeof schema)["apiMethodsTypeSchema"][Path]["api"]["action"]>[0] | string, headers?: Record<string, string>, options?: ExecuteOptions) => Promise<ExecuteResult<Result>>;
|
|
17
|
-
|
|
17
|
+
randParams: () => Promise<Parameters<ApiT["action"]>[0]>;
|
|
18
|
+
randOtherParams: <Path extends keyof (typeof schema)["apiMethodsTypeSchema"]>(path: Path) => Promise<Parameters<(typeof schema)["apiMethodsTypeSchema"][Path]['api']['action']>[0]>;
|
|
18
19
|
reject: (message?: string) => void;
|
|
19
20
|
}) => Promise<void> | void;
|
|
20
21
|
name: string;
|
package/kernel/milkio.ts
CHANGED
|
@@ -43,7 +43,8 @@ export async function createMilkioApp(MilkioAppOptions: MilkioAppOptions = {}) {
|
|
|
43
43
|
execute: _execute,
|
|
44
44
|
executeToJson: _executeToJson,
|
|
45
45
|
_executeCore,
|
|
46
|
-
_executeCoreToJson
|
|
46
|
+
_executeCoreToJson,
|
|
47
|
+
randParams: _randParams
|
|
47
48
|
}
|
|
48
49
|
|
|
49
50
|
if (MilkioAppOptions.bootstraps) {
|
|
@@ -163,7 +164,7 @@ async function _executeCore<Path extends keyof (typeof schema)["apiMethodsTypeSc
|
|
|
163
164
|
// check type
|
|
164
165
|
// @ts-ignore
|
|
165
166
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
166
|
-
_validate(await (await schema.apiValidator.validate[path]()).
|
|
167
|
+
params = _validate(await (await schema.apiValidator.validate[path]()).validateParams(params))
|
|
167
168
|
|
|
168
169
|
// execute api
|
|
169
170
|
let api: any
|
|
@@ -197,16 +198,21 @@ async function _executeCore<Path extends keyof (typeof schema)["apiMethodsTypeSc
|
|
|
197
198
|
|
|
198
199
|
async function _executeToJson<Path extends keyof (typeof schema)["apiMethodsTypeSchema"]>(path: Path, params: Parameters<(typeof schema)["apiMethodsTypeSchema"][Path]["api"]["action"]>[0] | string, headersInit: Record<string, string> | Headers = {}, options?: ExecuteOptions): Promise<string> {
|
|
199
200
|
const resultsRaw = await _execute(path, params, headersInit, options)
|
|
200
|
-
const results = await (await schema.apiValidator.validate[path]()).
|
|
201
|
+
const results = await (await schema.apiValidator.validate[path]()).validateResults(TSON.encode(resultsRaw))
|
|
201
202
|
return results
|
|
202
203
|
}
|
|
203
204
|
|
|
204
205
|
async function _executeCoreToJson<Path extends keyof (typeof schema)["apiMethodsTypeSchema"]>(path: Path, params: Parameters<(typeof schema)["apiMethodsTypeSchema"][Path]["api"]["action"]>[0] | string, headersInit: Record<string, string> | Headers = {}, options: ExecuteCoreOptions): Promise<string> {
|
|
205
206
|
const resultsRaw = await _executeCore(path, params, headersInit, options)
|
|
206
|
-
const results = await (await schema.apiValidator.validate[path]()).
|
|
207
|
+
const results = await (await schema.apiValidator.validate[path]()).validateResults(TSON.encode(resultsRaw))
|
|
207
208
|
return results
|
|
208
209
|
}
|
|
209
210
|
|
|
211
|
+
|
|
212
|
+
export async function _randParams<Path extends keyof (typeof schema)["apiMethodsTypeSchema"]>(path: Path): Promise<Parameters<(typeof schema)["apiMethodsTypeSchema"][Path]['api']['action']>[0]> {
|
|
213
|
+
return await (await schema.apiValidator.validate[path]()).randParams()
|
|
214
|
+
}
|
|
215
|
+
|
|
210
216
|
const apis = new Map<string, any>()
|
|
211
217
|
|
|
212
218
|
export type ExecuteResult<Result> = ExecuteResultSuccess<Result> | ExecuteResultFail;
|
|
@@ -244,4 +250,4 @@ export type ExecuteCoreOptions = Mixin<
|
|
|
244
250
|
logger: Logger;
|
|
245
251
|
onAfterHeaders?: (headers: Headers) => void | Promise<void>;
|
|
246
252
|
}
|
|
247
|
-
>;
|
|
253
|
+
>;
|
package/kernel/validate.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
|
|
1
3
|
import type { IValidation } from "typia"
|
|
2
4
|
import { reject } from "../kernel/fail"
|
|
3
5
|
|
|
4
|
-
export function _validate(validator: IValidation.IFailure | IValidation.ISuccess):
|
|
5
|
-
if (validator.success) return
|
|
6
|
+
export function _validate(validator: IValidation.IFailure | IValidation.ISuccess): any {
|
|
7
|
+
if (validator.success) return validator.data
|
|
6
8
|
const error = validator.errors[0]
|
|
7
9
|
|
|
8
10
|
throw reject("TYPE_SAFE_ERROR", {
|
package/package.json
CHANGED
|
@@ -12,7 +12,7 @@ export default async () => {
|
|
|
12
12
|
const paths = Object.keys(schema.default.apiMethodsSchema)
|
|
13
13
|
|
|
14
14
|
console.log('')
|
|
15
|
-
console.time(
|
|
15
|
+
console.time(`🌟 Cookbook Stage`)
|
|
16
16
|
|
|
17
17
|
const cookbook: Cookbook = {}
|
|
18
18
|
for (const path of paths) {
|
|
@@ -234,10 +234,10 @@ export default async () => {
|
|
|
234
234
|
})
|
|
235
235
|
)
|
|
236
236
|
|
|
237
|
-
console.timeEnd(
|
|
237
|
+
console.timeEnd(`🌟 Cookbook Stage`)
|
|
238
238
|
console.log(``)
|
|
239
239
|
|
|
240
|
-
console.time(
|
|
240
|
+
console.time(`🌟 Client Stage`)
|
|
241
241
|
await $`bun run ./node_modules/typescript/bin/tsc --outDir "./packages/client/project"`.quiet()
|
|
242
242
|
await Bun.build({
|
|
243
243
|
entrypoints: ["./packages/client/index.ts"],
|
|
@@ -245,7 +245,7 @@ export default async () => {
|
|
|
245
245
|
target: 'browser',
|
|
246
246
|
minify: true
|
|
247
247
|
})
|
|
248
|
-
console.timeEnd(
|
|
248
|
+
console.timeEnd(`🌟 Client Stage`)
|
|
249
249
|
console.log(``)
|
|
250
250
|
|
|
251
251
|
if (!existsSync(join(cwd(), "milkio.toml"))) return
|
|
@@ -254,8 +254,8 @@ export default async () => {
|
|
|
254
254
|
let i = 0
|
|
255
255
|
for (const command of milkioConfig.generate.significant) {
|
|
256
256
|
++i
|
|
257
|
-
console.time(
|
|
257
|
+
console.time(`🌟 Insignificant Stage (LINE ${i})`)
|
|
258
258
|
await $`${{ raw: command }}`
|
|
259
|
-
console.timeEnd(
|
|
259
|
+
console.timeEnd(`🌟 Insignificant Stage (LINE ${i})`)
|
|
260
260
|
}
|
|
261
261
|
}
|
|
@@ -50,7 +50,7 @@ export default async () => {
|
|
|
50
50
|
const appFiles = await Array.fromAsync(glob.scan({ cwd: join(cwd(), "src", "apps") }))
|
|
51
51
|
|
|
52
52
|
|
|
53
|
-
console.time(
|
|
53
|
+
console.time(`🌟 File Stage`)
|
|
54
54
|
|
|
55
55
|
|
|
56
56
|
for (const path of appFiles) {
|
|
@@ -95,10 +95,10 @@ import { type TSONEncode } from "@southern-aurora/tson";
|
|
|
95
95
|
import type * as <%= utils.camel(path.slice(0, -3).replaceAll('/', '$')) %> from '${importPath}/<%= path.slice(0, -3) %>';
|
|
96
96
|
|
|
97
97
|
type ParamsT = Parameters<typeof <%= utils.camel(path.replaceAll('/', '$').slice(0, -${3})) %>['api']['action']>[0];
|
|
98
|
-
export const
|
|
98
|
+
export const validateParams = async (params: any) => typia.misc.validatePrune<ParamsT>(params);
|
|
99
99
|
type ResultsT = Awaited<ReturnType<typeof <%= utils.camel(path.replaceAll('/', '$').slice(0, -${3})) %>['api']['action']>>;
|
|
100
|
-
export const
|
|
101
|
-
|
|
100
|
+
export const validateResults = async (results: any) => { _validate(typia.validate<TSONEncode<ExecuteResultSuccess<ResultsT>>>(results)); return typia.json.stringify<TSONEncode<ExecuteResultSuccess<ResultsT>>>(results); };
|
|
101
|
+
export const randParams = async () => typia.random<ParamsT>();
|
|
102
102
|
`.trim()
|
|
103
103
|
// export const paramsSchema = typia.json.application<[{ data: ParamsT }], "swagger">();
|
|
104
104
|
|
|
@@ -154,13 +154,13 @@ export default {
|
|
|
154
154
|
`.trim()
|
|
155
155
|
await writeFile(join(cwd(), "generated", "raw", "api-validator.ts"), ejs.render(apiValidatorTemplate, templateVars))
|
|
156
156
|
|
|
157
|
-
console.timeEnd(
|
|
157
|
+
console.timeEnd(`🌟 File Stage`)
|
|
158
158
|
console.log(``)
|
|
159
159
|
|
|
160
160
|
// typia
|
|
161
|
-
console.time(
|
|
161
|
+
console.time(`🌟 Typia Stage`)
|
|
162
162
|
await $`bun run ./node_modules/typia/lib/executable/typia.js generate --input generated/raw --output generated/products --project tsconfig.json`
|
|
163
|
-
console.timeEnd(
|
|
163
|
+
console.timeEnd(`🌟 Typia Stage`)
|
|
164
164
|
console.log(``)
|
|
165
165
|
|
|
166
166
|
if (!existsSync(join(cwd(), "milkio.toml"))) return
|
|
@@ -169,8 +169,8 @@ export default {
|
|
|
169
169
|
let i = 0
|
|
170
170
|
for (const command of milkioConfig.generate.significant) {
|
|
171
171
|
++i
|
|
172
|
-
console.time(
|
|
172
|
+
console.time(`🌟 Significant Stage (LINE ${i})`)
|
|
173
173
|
await $`${{ raw: command }}`
|
|
174
|
-
console.timeEnd(
|
|
174
|
+
console.timeEnd(`🌟 Significant Stage (LINE ${i})`)
|
|
175
175
|
}
|
|
176
176
|
}
|
package/templates/api.ts
CHANGED
|
@@ -5,45 +5,41 @@ await createTemplate(async (tools) => {
|
|
|
5
5
|
return {
|
|
6
6
|
path: join(tools.directory, `${tools.hyphen(tools.name)}.ts`),
|
|
7
7
|
content: `
|
|
8
|
-
import { defineApi, defineApiTest } from "milkio"
|
|
8
|
+
import { defineApi, defineApiTest } from "milkio"
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
|
-
* ${tools.
|
|
11
|
+
* ${tools.name} ${tools.src()}
|
|
12
12
|
*/
|
|
13
13
|
export const api = defineApi({
|
|
14
14
|
meta: {
|
|
15
|
-
//
|
|
15
|
+
// your meta..
|
|
16
16
|
},
|
|
17
17
|
async action(
|
|
18
18
|
params: {
|
|
19
|
-
//
|
|
19
|
+
// your params..
|
|
20
20
|
},
|
|
21
21
|
context
|
|
22
22
|
) {
|
|
23
|
-
const message = \`hello world
|
|
23
|
+
const message = \`hello world!\`
|
|
24
24
|
|
|
25
|
-
// ..
|
|
25
|
+
// your code..
|
|
26
26
|
|
|
27
27
|
return {
|
|
28
28
|
say: message
|
|
29
|
-
}
|
|
29
|
+
}
|
|
30
30
|
}
|
|
31
|
-
})
|
|
31
|
+
})
|
|
32
32
|
|
|
33
33
|
export const test = defineApiTest(api, [
|
|
34
34
|
{
|
|
35
35
|
name: "Basic",
|
|
36
36
|
handler: async (test) => {
|
|
37
|
-
const result = await test.execute(
|
|
38
|
-
//
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
// ..
|
|
37
|
+
const result = await test.execute(await test.randParams())
|
|
42
38
|
|
|
43
|
-
if (!result.success) return test.reject(\`The result was not success\`)
|
|
39
|
+
if (!result.success) return test.reject(\`The result was not success\`)
|
|
44
40
|
}
|
|
45
41
|
}
|
|
46
|
-
])
|
|
42
|
+
])
|
|
47
43
|
`.trim()
|
|
48
44
|
}
|
|
49
45
|
})
|
package/utils/create-template.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { camel, hump, hyphen } from "@poech/camel-hump-under"
|
|
|
4
4
|
export type CreateTemplateTools = {
|
|
5
5
|
name: string;
|
|
6
6
|
directory: string;
|
|
7
|
+
src: () => string;
|
|
7
8
|
camel: (str: string) => string;
|
|
8
9
|
hump: (str: string) => string;
|
|
9
10
|
hyphen: (str: string) => string;
|
|
@@ -23,6 +24,15 @@ export async function createTemplate(fn: CreateTemplateFn) {
|
|
|
23
24
|
const tools = {
|
|
24
25
|
name: argv[2],
|
|
25
26
|
directory: argv[3],
|
|
27
|
+
src: () => {
|
|
28
|
+
const patharr = argv[3].split("/")
|
|
29
|
+
const i = patharr.length - patharr.findIndex((str: string) => str.startsWith("src")) - 1
|
|
30
|
+
let path = ''
|
|
31
|
+
for (let j = 0; j < i; j++) {
|
|
32
|
+
path += "../"
|
|
33
|
+
}
|
|
34
|
+
return path
|
|
35
|
+
},
|
|
26
36
|
camel: (str: string) => camel(str).replaceAll("-", "").replaceAll("_", ""),
|
|
27
37
|
hump: (str: string) => hump(str).replaceAll("-", "").replaceAll("_", ""),
|
|
28
38
|
hyphen: (str: string) => hyphen(str).replaceAll("_", "")
|