milkio 0.0.13 → 0.0.14

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 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"}`)
@@ -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
- // execute
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
- // reject
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]()).params(params))
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]()).results(TSON.encode(resultsRaw))
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]()).results(TSON.encode(resultsRaw))
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
+ >;
@@ -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): void {
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
@@ -2,7 +2,7 @@
2
2
  "name": "milkio",
3
3
  "type": "module",
4
4
  "module": "index.ts",
5
- "version": "0.0.13",
5
+ "version": "0.0.14",
6
6
  "peerDependencies": {
7
7
  "typescript": "^5.4.2"
8
8
  },
@@ -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 params = async (params: any) => typia.misc.validatePrune<ParamsT>(params);
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 results = async (results: any) => { _validate(typia.validate<TSONEncode<ExecuteResultSuccess<ResultsT>>>(results)); return typia.json.stringify<TSONEncode<ExecuteResultSuccess<ResultsT>>>(results); };
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