@forklaunch/core 0.14.5 → 0.14.6
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/lib/mappers/index.d.mts
CHANGED
@@ -14,25 +14,43 @@ declare function responseMapper<SV extends AnySchemaValidator, DomainSchema exte
|
|
14
14
|
} & typeof mapperDefinition>;
|
15
15
|
|
16
16
|
/**
|
17
|
-
*
|
17
|
+
* A function type that, given an options object, returns a schema or schema-like object.
|
18
18
|
*
|
19
|
-
* @template
|
19
|
+
* @template Args - The type of the options object, typically an object with configuration or dependencies.
|
20
|
+
* @param {Args} options - The options to use for schema resolution (e.g., validator, uuidId, etc.).
|
21
|
+
* @returns {unknown} The resolved schema or schema-like object.
|
22
|
+
*/
|
23
|
+
type SchemaResolutionFunction<Args extends Record<string, unknown>> = (options: Args) => unknown;
|
24
|
+
|
25
|
+
/**
|
26
|
+
* Maps a set of service schema factories or pre-instantiated schemas to their resolved schemas using the provided arguments.
|
27
|
+
*
|
28
|
+
* This utility allows you to provide an object whose values are either:
|
29
|
+
* - Schema factory functions (SchemaResolutionFunction) that accept an options object (Args) and return a schema or schema group.
|
30
|
+
* - Already-instantiated schemas (IdiomaticSchema).
|
31
|
+
*
|
32
|
+
* Each factory function will be called with the provided `args` object, and the result will be included in the returned mapping.
|
33
|
+
* If a value is already a schema (not a function), it is returned as-is.
|
34
|
+
*
|
35
|
+
* @template SV - The schema validator type.
|
36
|
+
* @template T - An object whose values are either schema factory functions or instantiated schemas.
|
20
37
|
* @template Args - The type of the options object passed to each factory function (e.g., { validator, uuidId, ... }).
|
21
38
|
*
|
22
|
-
* @param {T} schemas - An object mapping schema names to factory functions
|
39
|
+
* @param {T} schemas - An object mapping schema names to either factory functions (SchemaResolutionFunction) or instantiated schemas (IdiomaticSchema).
|
23
40
|
* @param {Args} args - The options object to be passed to each schema factory function.
|
24
|
-
* @returns {{ [K in keyof T]: ReturnType<T[K]> }} An object mapping each schema name to its
|
41
|
+
* @returns {{ [K in keyof T]: T[K] extends SchemaResolutionFunction<Args> ? ReturnType<T[K]> : T[K] }} An object mapping each schema name to its resolved schema.
|
25
42
|
*
|
26
43
|
* @example
|
27
44
|
* const schemas = {
|
28
45
|
* UserSchemas: (opts) => createUserSchemas(opts),
|
29
|
-
* ProductSchemas: (opts) => createProductSchemas(opts)
|
46
|
+
* ProductSchemas: (opts) => createProductSchemas(opts),
|
47
|
+
* AlreadyInstantiated: someSchemaObject
|
30
48
|
* };
|
31
49
|
* const mapped = mapServiceSchemas(schemas, { validator: myValidator });
|
32
|
-
* // mapped.UserSchemas
|
50
|
+
* // mapped.UserSchemas and mapped.ProductSchemas are instantiated, mapped.AlreadyInstantiated is passed through
|
33
51
|
*/
|
34
|
-
declare function mapServiceSchemas<T extends Record<string,
|
35
|
-
[K in keyof T]: ReturnType<T[K]
|
52
|
+
declare function mapServiceSchemas<SV extends AnySchemaValidator, T extends Record<string, SchemaResolutionFunction<Args> | IdiomaticSchema<SV>>, Args extends Record<string, unknown>>(schemas: T, args: Args): {
|
53
|
+
[K in keyof T]: T[K] extends SchemaResolutionFunction<Args> ? ReturnType<T[K]> : T[K];
|
36
54
|
};
|
37
55
|
|
38
56
|
export { mapServiceSchemas, requestMapper, responseMapper };
|
package/lib/mappers/index.d.ts
CHANGED
@@ -14,25 +14,43 @@ declare function responseMapper<SV extends AnySchemaValidator, DomainSchema exte
|
|
14
14
|
} & typeof mapperDefinition>;
|
15
15
|
|
16
16
|
/**
|
17
|
-
*
|
17
|
+
* A function type that, given an options object, returns a schema or schema-like object.
|
18
18
|
*
|
19
|
-
* @template
|
19
|
+
* @template Args - The type of the options object, typically an object with configuration or dependencies.
|
20
|
+
* @param {Args} options - The options to use for schema resolution (e.g., validator, uuidId, etc.).
|
21
|
+
* @returns {unknown} The resolved schema or schema-like object.
|
22
|
+
*/
|
23
|
+
type SchemaResolutionFunction<Args extends Record<string, unknown>> = (options: Args) => unknown;
|
24
|
+
|
25
|
+
/**
|
26
|
+
* Maps a set of service schema factories or pre-instantiated schemas to their resolved schemas using the provided arguments.
|
27
|
+
*
|
28
|
+
* This utility allows you to provide an object whose values are either:
|
29
|
+
* - Schema factory functions (SchemaResolutionFunction) that accept an options object (Args) and return a schema or schema group.
|
30
|
+
* - Already-instantiated schemas (IdiomaticSchema).
|
31
|
+
*
|
32
|
+
* Each factory function will be called with the provided `args` object, and the result will be included in the returned mapping.
|
33
|
+
* If a value is already a schema (not a function), it is returned as-is.
|
34
|
+
*
|
35
|
+
* @template SV - The schema validator type.
|
36
|
+
* @template T - An object whose values are either schema factory functions or instantiated schemas.
|
20
37
|
* @template Args - The type of the options object passed to each factory function (e.g., { validator, uuidId, ... }).
|
21
38
|
*
|
22
|
-
* @param {T} schemas - An object mapping schema names to factory functions
|
39
|
+
* @param {T} schemas - An object mapping schema names to either factory functions (SchemaResolutionFunction) or instantiated schemas (IdiomaticSchema).
|
23
40
|
* @param {Args} args - The options object to be passed to each schema factory function.
|
24
|
-
* @returns {{ [K in keyof T]: ReturnType<T[K]> }} An object mapping each schema name to its
|
41
|
+
* @returns {{ [K in keyof T]: T[K] extends SchemaResolutionFunction<Args> ? ReturnType<T[K]> : T[K] }} An object mapping each schema name to its resolved schema.
|
25
42
|
*
|
26
43
|
* @example
|
27
44
|
* const schemas = {
|
28
45
|
* UserSchemas: (opts) => createUserSchemas(opts),
|
29
|
-
* ProductSchemas: (opts) => createProductSchemas(opts)
|
46
|
+
* ProductSchemas: (opts) => createProductSchemas(opts),
|
47
|
+
* AlreadyInstantiated: someSchemaObject
|
30
48
|
* };
|
31
49
|
* const mapped = mapServiceSchemas(schemas, { validator: myValidator });
|
32
|
-
* // mapped.UserSchemas
|
50
|
+
* // mapped.UserSchemas and mapped.ProductSchemas are instantiated, mapped.AlreadyInstantiated is passed through
|
33
51
|
*/
|
34
|
-
declare function mapServiceSchemas<T extends Record<string,
|
35
|
-
[K in keyof T]: ReturnType<T[K]
|
52
|
+
declare function mapServiceSchemas<SV extends AnySchemaValidator, T extends Record<string, SchemaResolutionFunction<Args> | IdiomaticSchema<SV>>, Args extends Record<string, unknown>>(schemas: T, args: Args): {
|
53
|
+
[K in keyof T]: T[K] extends SchemaResolutionFunction<Args> ? ReturnType<T[K]> : T[K];
|
36
54
|
};
|
37
55
|
|
38
56
|
export { mapServiceSchemas, requestMapper, responseMapper };
|
package/lib/mappers/index.js
CHANGED
@@ -61,10 +61,18 @@ function responseMapper(schemaValidator, domainSchema, _entityConstructor, mappe
|
|
61
61
|
};
|
62
62
|
}
|
63
63
|
|
64
|
+
// src/mappers/guards/isSchemaResolutionFunction.ts
|
65
|
+
function isSchemaResolutionFunction(value) {
|
66
|
+
return typeof value === "function";
|
67
|
+
}
|
68
|
+
|
64
69
|
// src/mappers/mapServiceSchemas.ts
|
65
70
|
function mapServiceSchemas(schemas, args) {
|
66
71
|
return Object.fromEntries(
|
67
|
-
Object.entries(schemas).map(([key, value]) => [
|
72
|
+
Object.entries(schemas).map(([key, value]) => [
|
73
|
+
key,
|
74
|
+
isSchemaResolutionFunction(value) ? value(args) : value
|
75
|
+
])
|
68
76
|
);
|
69
77
|
}
|
70
78
|
// Annotate the CommonJS export names for ESM import in node:
|
package/lib/mappers/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/mappers/index.ts","../../src/mappers/mapper.ts","../../src/mappers/mapServiceSchemas.ts"],"sourcesContent":["export * from './mapper';\nexport * from './mapServiceSchemas';\n","import { Prettify } from '@forklaunch/common';\nimport {\n AnySchemaValidator,\n IdiomaticSchema,\n prettyPrintParseErrors,\n Schema,\n SchemaValidator\n} from '@forklaunch/validator';\nimport { Constructor } from '@mikro-orm/core';\n\nexport function requestMapper<\n SV extends AnySchemaValidator,\n DomainSchema extends IdiomaticSchema<SV>,\n Entity,\n AdditionalArgs extends unknown[] = []\n>(\n schemaValidator: SV,\n domainSchema: DomainSchema,\n _entityConstructor: Constructor<Entity>,\n mapperDefinition: {\n toEntity: (\n dto: Schema<DomainSchema, SV>,\n ...args: AdditionalArgs\n ) => Promise<Entity>;\n }\n): {\n schema: DomainSchema;\n} & typeof mapperDefinition {\n const sv = schemaValidator as SchemaValidator;\n return {\n ...mapperDefinition,\n schema: domainSchema,\n\n toEntity: async (\n dto: Schema<DomainSchema, SV>,\n ...args: AdditionalArgs\n ) => {\n const parsedSchema = sv.parse(sv.schemify(domainSchema), dto);\n if (!parsedSchema.ok) {\n throw new Error(prettyPrintParseErrors(parsedSchema.errors, 'DTO'));\n }\n return mapperDefinition.toEntity(\n dto as Schema<DomainSchema, SV>,\n ...(args as AdditionalArgs)\n );\n }\n };\n}\n\nexport function responseMapper<\n SV extends AnySchemaValidator,\n DomainSchema extends IdiomaticSchema<SV>,\n Entity,\n AdditionalArgs extends unknown[] = []\n>(\n schemaValidator: SV,\n domainSchema: DomainSchema,\n _entityConstructor: Constructor<Entity>,\n mapperDefinition: {\n toDto: (\n entity: Entity,\n ...args: AdditionalArgs\n ) => Promise<Schema<DomainSchema, SV>>;\n }\n): Prettify<\n {\n schema: DomainSchema;\n } & typeof mapperDefinition\n> {\n const sv = schemaValidator as SchemaValidator;\n return {\n ...mapperDefinition,\n schema: domainSchema,\n\n toDto: async (entity: Entity, ...args: AdditionalArgs) => {\n const domain = await mapperDefinition.toDto(entity, ...args);\n const parsedSchema = sv.parse(sv.schemify(domainSchema), domain);\n if (!parsedSchema.ok) {\n throw new Error(prettyPrintParseErrors(parsedSchema.errors, 'DTO'));\n }\n return domain;\n }\n };\n}\n","/**\n * Maps a set of service schema
|
1
|
+
{"version":3,"sources":["../../src/mappers/index.ts","../../src/mappers/mapper.ts","../../src/mappers/guards/isSchemaResolutionFunction.ts","../../src/mappers/mapServiceSchemas.ts"],"sourcesContent":["export * from './mapper';\nexport * from './mapServiceSchemas';\n","import { Prettify } from '@forklaunch/common';\nimport {\n AnySchemaValidator,\n IdiomaticSchema,\n prettyPrintParseErrors,\n Schema,\n SchemaValidator\n} from '@forklaunch/validator';\nimport { Constructor } from '@mikro-orm/core';\n\nexport function requestMapper<\n SV extends AnySchemaValidator,\n DomainSchema extends IdiomaticSchema<SV>,\n Entity,\n AdditionalArgs extends unknown[] = []\n>(\n schemaValidator: SV,\n domainSchema: DomainSchema,\n _entityConstructor: Constructor<Entity>,\n mapperDefinition: {\n toEntity: (\n dto: Schema<DomainSchema, SV>,\n ...args: AdditionalArgs\n ) => Promise<Entity>;\n }\n): {\n schema: DomainSchema;\n} & typeof mapperDefinition {\n const sv = schemaValidator as SchemaValidator;\n return {\n ...mapperDefinition,\n schema: domainSchema,\n\n toEntity: async (\n dto: Schema<DomainSchema, SV>,\n ...args: AdditionalArgs\n ) => {\n const parsedSchema = sv.parse(sv.schemify(domainSchema), dto);\n if (!parsedSchema.ok) {\n throw new Error(prettyPrintParseErrors(parsedSchema.errors, 'DTO'));\n }\n return mapperDefinition.toEntity(\n dto as Schema<DomainSchema, SV>,\n ...(args as AdditionalArgs)\n );\n }\n };\n}\n\nexport function responseMapper<\n SV extends AnySchemaValidator,\n DomainSchema extends IdiomaticSchema<SV>,\n Entity,\n AdditionalArgs extends unknown[] = []\n>(\n schemaValidator: SV,\n domainSchema: DomainSchema,\n _entityConstructor: Constructor<Entity>,\n mapperDefinition: {\n toDto: (\n entity: Entity,\n ...args: AdditionalArgs\n ) => Promise<Schema<DomainSchema, SV>>;\n }\n): Prettify<\n {\n schema: DomainSchema;\n } & typeof mapperDefinition\n> {\n const sv = schemaValidator as SchemaValidator;\n return {\n ...mapperDefinition,\n schema: domainSchema,\n\n toDto: async (entity: Entity, ...args: AdditionalArgs) => {\n const domain = await mapperDefinition.toDto(entity, ...args);\n const parsedSchema = sv.parse(sv.schemify(domainSchema), domain);\n if (!parsedSchema.ok) {\n throw new Error(prettyPrintParseErrors(parsedSchema.errors, 'DTO'));\n }\n return domain;\n }\n };\n}\n","import { SchemaResolutionFunction } from '../types/schemaResolution.types';\n\n/**\n * Type guard to determine if a value is a SchemaResolutionFunction.\n *\n * @template Args - The type of the options object expected by the schema resolution function.\n * @param {unknown} value - The value to check.\n * @returns {value is SchemaResolutionFunction<Args>} True if the value is a function (assumed to be a SchemaResolutionFunction), false otherwise.\n */\nexport function isSchemaResolutionFunction<\n Args extends Record<string, unknown>\n>(value: unknown): value is SchemaResolutionFunction<Args> {\n return typeof value === 'function';\n}\n","import { AnySchemaValidator, IdiomaticSchema } from '@forklaunch/validator';\nimport { isSchemaResolutionFunction } from './guards/isSchemaResolutionFunction';\nimport { SchemaResolutionFunction } from './types/schemaResolution.types';\n\n/**\n * Maps a set of service schema factories or pre-instantiated schemas to their resolved schemas using the provided arguments.\n *\n * This utility allows you to provide an object whose values are either:\n * - Schema factory functions (SchemaResolutionFunction) that accept an options object (Args) and return a schema or schema group.\n * - Already-instantiated schemas (IdiomaticSchema).\n *\n * Each factory function will be called with the provided `args` object, and the result will be included in the returned mapping.\n * If a value is already a schema (not a function), it is returned as-is.\n *\n * @template SV - The schema validator type.\n * @template T - An object whose values are either schema factory functions or instantiated schemas.\n * @template Args - The type of the options object passed to each factory function (e.g., { validator, uuidId, ... }).\n *\n * @param {T} schemas - An object mapping schema names to either factory functions (SchemaResolutionFunction) or instantiated schemas (IdiomaticSchema).\n * @param {Args} args - The options object to be passed to each schema factory function.\n * @returns {{ [K in keyof T]: T[K] extends SchemaResolutionFunction<Args> ? ReturnType<T[K]> : T[K] }} An object mapping each schema name to its resolved schema.\n *\n * @example\n * const schemas = {\n * UserSchemas: (opts) => createUserSchemas(opts),\n * ProductSchemas: (opts) => createProductSchemas(opts),\n * AlreadyInstantiated: someSchemaObject\n * };\n * const mapped = mapServiceSchemas(schemas, { validator: myValidator });\n * // mapped.UserSchemas and mapped.ProductSchemas are instantiated, mapped.AlreadyInstantiated is passed through\n */\nexport function mapServiceSchemas<\n SV extends AnySchemaValidator,\n T extends Record<\n string,\n SchemaResolutionFunction<Args> | IdiomaticSchema<SV>\n >,\n Args extends Record<string, unknown>\n>(\n schemas: T,\n args: Args\n): {\n [K in keyof T]: T[K] extends SchemaResolutionFunction<Args>\n ? ReturnType<T[K]>\n : T[K];\n} {\n return Object.fromEntries(\n Object.entries(schemas).map(([key, value]) => [\n key,\n isSchemaResolutionFunction(value) ? value(args) : value\n ])\n ) as {\n [K in keyof T]: T[K] extends SchemaResolutionFunction<Args>\n ? ReturnType<T[K]>\n : T[K];\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,uBAMO;AAGA,SAAS,cAMd,iBACA,cACA,oBACA,kBAQ0B;AAC1B,QAAM,KAAK;AACX,SAAO;AAAA,IACL,GAAG;AAAA,IACH,QAAQ;AAAA,IAER,UAAU,OACR,QACG,SACA;AACH,YAAM,eAAe,GAAG,MAAM,GAAG,SAAS,YAAY,GAAG,GAAG;AAC5D,UAAI,CAAC,aAAa,IAAI;AACpB,cAAM,IAAI,UAAM,yCAAuB,aAAa,QAAQ,KAAK,CAAC;AAAA,MACpE;AACA,aAAO,iBAAiB;AAAA,QACtB;AAAA,QACA,GAAI;AAAA,MACN;AAAA,IACF;AAAA,EACF;AACF;AAEO,SAAS,eAMd,iBACA,cACA,oBACA,kBAUA;AACA,QAAM,KAAK;AACX,SAAO;AAAA,IACL,GAAG;AAAA,IACH,QAAQ;AAAA,IAER,OAAO,OAAO,WAAmB,SAAyB;AACxD,YAAM,SAAS,MAAM,iBAAiB,MAAM,QAAQ,GAAG,IAAI;AAC3D,YAAM,eAAe,GAAG,MAAM,GAAG,SAAS,YAAY,GAAG,MAAM;AAC/D,UAAI,CAAC,aAAa,IAAI;AACpB,cAAM,IAAI,UAAM,yCAAuB,aAAa,QAAQ,KAAK,CAAC;AAAA,MACpE;AACA,aAAO;AAAA,IACT;AAAA,EACF;AACF;;;AC1EO,SAAS,2BAEd,OAAyD;AACzD,SAAO,OAAO,UAAU;AAC1B;;;ACkBO,SAAS,kBAQd,SACA,MAKA;AACA,SAAO,OAAO;AAAA,IACZ,OAAO,QAAQ,OAAO,EAAE,IAAI,CAAC,CAAC,KAAK,KAAK,MAAM;AAAA,MAC5C;AAAA,MACA,2BAA2B,KAAK,IAAI,MAAM,IAAI,IAAI;AAAA,IACpD,CAAC;AAAA,EACH;AAKF;","names":[]}
|
package/lib/mappers/index.mjs
CHANGED
@@ -35,10 +35,18 @@ function responseMapper(schemaValidator, domainSchema, _entityConstructor, mappe
|
|
35
35
|
};
|
36
36
|
}
|
37
37
|
|
38
|
+
// src/mappers/guards/isSchemaResolutionFunction.ts
|
39
|
+
function isSchemaResolutionFunction(value) {
|
40
|
+
return typeof value === "function";
|
41
|
+
}
|
42
|
+
|
38
43
|
// src/mappers/mapServiceSchemas.ts
|
39
44
|
function mapServiceSchemas(schemas, args) {
|
40
45
|
return Object.fromEntries(
|
41
|
-
Object.entries(schemas).map(([key, value]) => [
|
46
|
+
Object.entries(schemas).map(([key, value]) => [
|
47
|
+
key,
|
48
|
+
isSchemaResolutionFunction(value) ? value(args) : value
|
49
|
+
])
|
42
50
|
);
|
43
51
|
}
|
44
52
|
export {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/mappers/mapper.ts","../../src/mappers/mapServiceSchemas.ts"],"sourcesContent":["import { Prettify } from '@forklaunch/common';\nimport {\n AnySchemaValidator,\n IdiomaticSchema,\n prettyPrintParseErrors,\n Schema,\n SchemaValidator\n} from '@forklaunch/validator';\nimport { Constructor } from '@mikro-orm/core';\n\nexport function requestMapper<\n SV extends AnySchemaValidator,\n DomainSchema extends IdiomaticSchema<SV>,\n Entity,\n AdditionalArgs extends unknown[] = []\n>(\n schemaValidator: SV,\n domainSchema: DomainSchema,\n _entityConstructor: Constructor<Entity>,\n mapperDefinition: {\n toEntity: (\n dto: Schema<DomainSchema, SV>,\n ...args: AdditionalArgs\n ) => Promise<Entity>;\n }\n): {\n schema: DomainSchema;\n} & typeof mapperDefinition {\n const sv = schemaValidator as SchemaValidator;\n return {\n ...mapperDefinition,\n schema: domainSchema,\n\n toEntity: async (\n dto: Schema<DomainSchema, SV>,\n ...args: AdditionalArgs\n ) => {\n const parsedSchema = sv.parse(sv.schemify(domainSchema), dto);\n if (!parsedSchema.ok) {\n throw new Error(prettyPrintParseErrors(parsedSchema.errors, 'DTO'));\n }\n return mapperDefinition.toEntity(\n dto as Schema<DomainSchema, SV>,\n ...(args as AdditionalArgs)\n );\n }\n };\n}\n\nexport function responseMapper<\n SV extends AnySchemaValidator,\n DomainSchema extends IdiomaticSchema<SV>,\n Entity,\n AdditionalArgs extends unknown[] = []\n>(\n schemaValidator: SV,\n domainSchema: DomainSchema,\n _entityConstructor: Constructor<Entity>,\n mapperDefinition: {\n toDto: (\n entity: Entity,\n ...args: AdditionalArgs\n ) => Promise<Schema<DomainSchema, SV>>;\n }\n): Prettify<\n {\n schema: DomainSchema;\n } & typeof mapperDefinition\n> {\n const sv = schemaValidator as SchemaValidator;\n return {\n ...mapperDefinition,\n schema: domainSchema,\n\n toDto: async (entity: Entity, ...args: AdditionalArgs) => {\n const domain = await mapperDefinition.toDto(entity, ...args);\n const parsedSchema = sv.parse(sv.schemify(domainSchema), domain);\n if (!parsedSchema.ok) {\n throw new Error(prettyPrintParseErrors(parsedSchema.errors, 'DTO'));\n }\n return domain;\n }\n };\n}\n","/**\n * Maps a set of service schema
|
1
|
+
{"version":3,"sources":["../../src/mappers/mapper.ts","../../src/mappers/guards/isSchemaResolutionFunction.ts","../../src/mappers/mapServiceSchemas.ts"],"sourcesContent":["import { Prettify } from '@forklaunch/common';\nimport {\n AnySchemaValidator,\n IdiomaticSchema,\n prettyPrintParseErrors,\n Schema,\n SchemaValidator\n} from '@forklaunch/validator';\nimport { Constructor } from '@mikro-orm/core';\n\nexport function requestMapper<\n SV extends AnySchemaValidator,\n DomainSchema extends IdiomaticSchema<SV>,\n Entity,\n AdditionalArgs extends unknown[] = []\n>(\n schemaValidator: SV,\n domainSchema: DomainSchema,\n _entityConstructor: Constructor<Entity>,\n mapperDefinition: {\n toEntity: (\n dto: Schema<DomainSchema, SV>,\n ...args: AdditionalArgs\n ) => Promise<Entity>;\n }\n): {\n schema: DomainSchema;\n} & typeof mapperDefinition {\n const sv = schemaValidator as SchemaValidator;\n return {\n ...mapperDefinition,\n schema: domainSchema,\n\n toEntity: async (\n dto: Schema<DomainSchema, SV>,\n ...args: AdditionalArgs\n ) => {\n const parsedSchema = sv.parse(sv.schemify(domainSchema), dto);\n if (!parsedSchema.ok) {\n throw new Error(prettyPrintParseErrors(parsedSchema.errors, 'DTO'));\n }\n return mapperDefinition.toEntity(\n dto as Schema<DomainSchema, SV>,\n ...(args as AdditionalArgs)\n );\n }\n };\n}\n\nexport function responseMapper<\n SV extends AnySchemaValidator,\n DomainSchema extends IdiomaticSchema<SV>,\n Entity,\n AdditionalArgs extends unknown[] = []\n>(\n schemaValidator: SV,\n domainSchema: DomainSchema,\n _entityConstructor: Constructor<Entity>,\n mapperDefinition: {\n toDto: (\n entity: Entity,\n ...args: AdditionalArgs\n ) => Promise<Schema<DomainSchema, SV>>;\n }\n): Prettify<\n {\n schema: DomainSchema;\n } & typeof mapperDefinition\n> {\n const sv = schemaValidator as SchemaValidator;\n return {\n ...mapperDefinition,\n schema: domainSchema,\n\n toDto: async (entity: Entity, ...args: AdditionalArgs) => {\n const domain = await mapperDefinition.toDto(entity, ...args);\n const parsedSchema = sv.parse(sv.schemify(domainSchema), domain);\n if (!parsedSchema.ok) {\n throw new Error(prettyPrintParseErrors(parsedSchema.errors, 'DTO'));\n }\n return domain;\n }\n };\n}\n","import { SchemaResolutionFunction } from '../types/schemaResolution.types';\n\n/**\n * Type guard to determine if a value is a SchemaResolutionFunction.\n *\n * @template Args - The type of the options object expected by the schema resolution function.\n * @param {unknown} value - The value to check.\n * @returns {value is SchemaResolutionFunction<Args>} True if the value is a function (assumed to be a SchemaResolutionFunction), false otherwise.\n */\nexport function isSchemaResolutionFunction<\n Args extends Record<string, unknown>\n>(value: unknown): value is SchemaResolutionFunction<Args> {\n return typeof value === 'function';\n}\n","import { AnySchemaValidator, IdiomaticSchema } from '@forklaunch/validator';\nimport { isSchemaResolutionFunction } from './guards/isSchemaResolutionFunction';\nimport { SchemaResolutionFunction } from './types/schemaResolution.types';\n\n/**\n * Maps a set of service schema factories or pre-instantiated schemas to their resolved schemas using the provided arguments.\n *\n * This utility allows you to provide an object whose values are either:\n * - Schema factory functions (SchemaResolutionFunction) that accept an options object (Args) and return a schema or schema group.\n * - Already-instantiated schemas (IdiomaticSchema).\n *\n * Each factory function will be called with the provided `args` object, and the result will be included in the returned mapping.\n * If a value is already a schema (not a function), it is returned as-is.\n *\n * @template SV - The schema validator type.\n * @template T - An object whose values are either schema factory functions or instantiated schemas.\n * @template Args - The type of the options object passed to each factory function (e.g., { validator, uuidId, ... }).\n *\n * @param {T} schemas - An object mapping schema names to either factory functions (SchemaResolutionFunction) or instantiated schemas (IdiomaticSchema).\n * @param {Args} args - The options object to be passed to each schema factory function.\n * @returns {{ [K in keyof T]: T[K] extends SchemaResolutionFunction<Args> ? ReturnType<T[K]> : T[K] }} An object mapping each schema name to its resolved schema.\n *\n * @example\n * const schemas = {\n * UserSchemas: (opts) => createUserSchemas(opts),\n * ProductSchemas: (opts) => createProductSchemas(opts),\n * AlreadyInstantiated: someSchemaObject\n * };\n * const mapped = mapServiceSchemas(schemas, { validator: myValidator });\n * // mapped.UserSchemas and mapped.ProductSchemas are instantiated, mapped.AlreadyInstantiated is passed through\n */\nexport function mapServiceSchemas<\n SV extends AnySchemaValidator,\n T extends Record<\n string,\n SchemaResolutionFunction<Args> | IdiomaticSchema<SV>\n >,\n Args extends Record<string, unknown>\n>(\n schemas: T,\n args: Args\n): {\n [K in keyof T]: T[K] extends SchemaResolutionFunction<Args>\n ? ReturnType<T[K]>\n : T[K];\n} {\n return Object.fromEntries(\n Object.entries(schemas).map(([key, value]) => [\n key,\n isSchemaResolutionFunction(value) ? value(args) : value\n ])\n ) as {\n [K in keyof T]: T[K] extends SchemaResolutionFunction<Args>\n ? ReturnType<T[K]>\n : T[K];\n };\n}\n"],"mappings":";AACA;AAAA,EAGE;AAAA,OAGK;AAGA,SAAS,cAMd,iBACA,cACA,oBACA,kBAQ0B;AAC1B,QAAM,KAAK;AACX,SAAO;AAAA,IACL,GAAG;AAAA,IACH,QAAQ;AAAA,IAER,UAAU,OACR,QACG,SACA;AACH,YAAM,eAAe,GAAG,MAAM,GAAG,SAAS,YAAY,GAAG,GAAG;AAC5D,UAAI,CAAC,aAAa,IAAI;AACpB,cAAM,IAAI,MAAM,uBAAuB,aAAa,QAAQ,KAAK,CAAC;AAAA,MACpE;AACA,aAAO,iBAAiB;AAAA,QACtB;AAAA,QACA,GAAI;AAAA,MACN;AAAA,IACF;AAAA,EACF;AACF;AAEO,SAAS,eAMd,iBACA,cACA,oBACA,kBAUA;AACA,QAAM,KAAK;AACX,SAAO;AAAA,IACL,GAAG;AAAA,IACH,QAAQ;AAAA,IAER,OAAO,OAAO,WAAmB,SAAyB;AACxD,YAAM,SAAS,MAAM,iBAAiB,MAAM,QAAQ,GAAG,IAAI;AAC3D,YAAM,eAAe,GAAG,MAAM,GAAG,SAAS,YAAY,GAAG,MAAM;AAC/D,UAAI,CAAC,aAAa,IAAI;AACpB,cAAM,IAAI,MAAM,uBAAuB,aAAa,QAAQ,KAAK,CAAC;AAAA,MACpE;AACA,aAAO;AAAA,IACT;AAAA,EACF;AACF;;;AC1EO,SAAS,2BAEd,OAAyD;AACzD,SAAO,OAAO,UAAU;AAC1B;;;ACkBO,SAAS,kBAQd,SACA,MAKA;AACA,SAAO,OAAO;AAAA,IACZ,OAAO,QAAQ,OAAO,EAAE,IAAI,CAAC,CAAC,KAAK,KAAK,MAAM;AAAA,MAC5C;AAAA,MACA,2BAA2B,KAAK,IAAI,MAAM,IAAI,IAAI;AAAA,IACpD,CAAC;AAAA,EACH;AAKF;","names":[]}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@forklaunch/core",
|
3
|
-
"version": "0.14.
|
3
|
+
"version": "0.14.6",
|
4
4
|
"description": "forklaunch-js core package. Contains useful building blocks.",
|
5
5
|
"homepage": "https://github.com/forklaunch/forklaunch-js#readme",
|
6
6
|
"bugs": {
|
@@ -91,8 +91,8 @@
|
|
91
91
|
"pino-pretty": "^13.1.1",
|
92
92
|
"redis": "^5.8.2",
|
93
93
|
"uuid": "^11.1.0",
|
94
|
-
"@forklaunch/common": "0.6.
|
95
|
-
"@forklaunch/validator": "0.10.
|
94
|
+
"@forklaunch/common": "0.6.5",
|
95
|
+
"@forklaunch/validator": "0.10.5"
|
96
96
|
},
|
97
97
|
"devDependencies": {
|
98
98
|
"@eslint/js": "^9.34.0",
|