Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ exports[`integration 1`] = `
"type": "string",
},
"id": {
"pattern": "^(?:user\\-)(?:-?[0-9]+(?:\\.[0-9]+)?)(?:\\-db1)$",
"pattern": "^(?:user\\-)(?:(?:[+-]?(?:(?:[0-9]+(?:\\.[0-9]*)?)|(?:\\.[0-9]+))(?:[eE][+-]?[0-9]+)?)|(?:0(?:[xX][0-9a-fA-F]+|[bB][01]+|[oO][0-7]+)))(?:\\-db1)$",
"type": "string",
},
"identifiedDataParser": {
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@
"vitest": "4.1.8"
},
"peerDependencies": {
"@duplojs/server-utils": ">=0.4.0 < 1.0.0",
"@duplojs/utils": ">=1.9.1 <2.0.0"
"@duplojs/server-utils": ">=0.4.2 < 1.0.0",
"@duplojs/utils": ">=1.10.1 <2.0.0"
},
"dependencies": {
"typescript": "5.9.2"
Expand Down
9 changes: 9 additions & 0 deletions scripts/finder/researcher/defaults/errorHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { DP } from "@duplojs/utils";
import { createResearcher } from "../create";

export const errorHandlerResearcher = createResearcher(
DP.errorHandlerKind.has,
(dataParser, params) => {
params.find(dataParser.definition.inner);
},
);
3 changes: 3 additions & 0 deletions scripts/finder/researcher/defaults/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export * from "./templateLiteral";
export * from "./transform";
export * from "./tuple";
export * from "./union";
export * from "./errorHandler";

import type { createResearcher } from "../create";
import { arrayResearcher } from "./array";
Expand All @@ -24,6 +25,7 @@ import { templateLiteralResearcher } from "./templateLiteral";
import { transformResearcher } from "./transform";
import { tupleResearcher } from "./tuple";
import { unionResearcher } from "./union";
import { errorHandlerResearcher } from "./errorHandler";

export const defaultResearchers = [
arrayResearcher,
Expand All @@ -38,4 +40,5 @@ export const defaultResearchers = [
transformResearcher,
tupleResearcher,
unionResearcher,
errorHandlerResearcher,
] as const satisfies readonly ReturnType<typeof createResearcher>[];
5 changes: 3 additions & 2 deletions scripts/toDataParser/buildContext.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DP, E, unwrap } from "@duplojs/utils";
import { createIdentifier, type createTransformer, transformer, type MapContext, type TransformerHook, type DataParserErrorEither, type DataParserNotSupportedEither, type DataParserGetDefinitionErrorEither, type ToTypescriptDataParserErrorEither, type ToTypescriptDataParserNotSupportedEither, type DependenciesContext } from "./dataParserTransformer";
import { createIdentifier, type createTransformer, transformer, type MapContext, type TransformerHook, type DataParserErrorEither, type DataParserNotSupportedEither, type DataParserGetDefinitionErrorEither, type ToTypescriptDataParserErrorEither, type ToTypescriptDataParserNotSupportedEither, type DependenciesContext, type ToTypescriptCheckerErrorEither } from "./dataParserTransformer";
import type * as TST from "@scripts/toTypescript";
import { getRecursiveDataParser } from "@scripts/utils";
import { factory } from "typescript";
Expand All @@ -19,10 +19,10 @@ export interface BuildContextParams {
readonly dataParserTransformers: readonly ReturnType<typeof createTransformer>[];
readonly checkerTransformers: readonly ReturnType<typeof createCheckerTransformer>[];
readonly typescriptTransformers: readonly ReturnType<typeof TST.createTransformer>[];
readonly typescriptCheckerRefiner?: readonly ReturnType<typeof TST.createCheckerRefiner>[];
readonly context?: MapContext;
readonly typescriptContext?: TST.MapContext;
readonly importContext?: TST.MapImportContext;

readonly importMode?: ImportMode;
readonly hooks?: readonly TransformerHook[];
readonly toTypescript?: {
Expand All @@ -41,6 +41,7 @@ export function buildContext(
| DataParserGetDefinitionErrorEither
| ToTypescriptDataParserNotSupportedEither
| ToTypescriptDataParserErrorEither
| ToTypescriptCheckerErrorEither
) {
const context: MapContext = params.context ?? new Map();
const typescriptContext: TST.MapContext = params.typescriptContext ?? new Map();
Expand Down
45 changes: 9 additions & 36 deletions scripts/toDataParser/checkerTransformer/create.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import type { CallExpression, ObjectLiteralExpression, PropertyAssignment } from "typescript";
import type { CallExpression, Identifier, ObjectLiteralExpression, PropertyAssignment } from "typescript";
import { type DP, E } from "@duplojs/utils";
import type { DServerDataParser } from "@duplojs/server-utils";
import type * as TST from "@scripts/toTypescript";

export type CheckerTransformerSuccessEither = E.Right<"buildSuccess", CallExpression>;
export type CheckerTransformerSuccessEither = E.Right<"buildSuccess", CallExpression | Identifier>;

export type CheckerTransformerCheckerNotSupportedEither = E.Left<"checkerNotSupport", DP.DataParserChecker>;
export type CheckerTransformerCheckerNotSupportedEither = E.Left<"checkerNotSupport", DP.DataParserCheckers>;

export type CheckerTransformerBuildErrorEither = E.Left<"buildCheckerError", DP.DataParserChecker>;
export type CheckerTransformerBuildErrorEither = E.Left<"buildCheckerError", DP.DataParserCheckers>;

export type CheckerTransformerEither =
| CheckerTransformerSuccessEither
Expand All @@ -18,7 +17,7 @@ export interface CheckerTransformerParams {
readonly importContext: TST.MapImportContext;

success(
result: CallExpression,
result: CallExpression | Identifier,
): CheckerTransformerSuccessEither;
buildError(): CheckerTransformerBuildErrorEither;
addImport(path: string, typeName: string, type?: "default" | "namespace" | "direct"): void;
Expand All @@ -27,47 +26,21 @@ export interface CheckerTransformerParams {
): readonly [ObjectLiteralExpression] | readonly [];
}

/**
* @deprecated
*/
export type DataParserCheckers = (
| DP.DataParserChecker
| DP.DataParserCheckerArrayMax
| DP.DataParserCheckerArrayMin
| DP.DataParserCheckerBigIntMax
| DP.DataParserCheckerBigIntMin
| DP.DataParserCheckerNumberMax
| DP.DataParserCheckerNumberMin
| DP.DataParserCheckerInt
| DP.DataParserCheckerStringMax
| DP.DataParserCheckerStringMin
| DP.DataParserCheckerEmail
| DP.DataParserCheckerRegex
| DP.DataParserCheckerUrl
| DP.DataParserCheckerUuid
| DP.DataParserCheckerRefine
| DP.DataParserCheckerTimeMin
| DP.DataParserCheckerTimeMax
| DServerDataParser.DataParserCheckerFileExist
| DServerDataParser.DataParserCheckerFileMimeType
| DServerDataParser.DataParserCheckerFileSize
);

export type CheckerTransformerBuildFunction<
GenericChecker extends DataParserCheckers = DataParserCheckers,
GenericChecker extends DP.DataParserCheckers = DP.DataParserCheckers,
> = (
checker: GenericChecker,
params: CheckerTransformerParams,
) => CheckerTransformerEither;

export function createCheckerTransformer<
GenericChecker extends DataParserCheckers,
GenericChecker extends DP.DataParserCheckers,
>(
support: (checker: DataParserCheckers) => checker is GenericChecker,
support: (checker: DP.DataParserCheckers) => checker is GenericChecker,
builder: CheckerTransformerBuildFunction<GenericChecker>,
) {
return (
checker: DataParserCheckers,
checker: DP.DataParserCheckers,
params: CheckerTransformerParams,
): CheckerTransformerEither => support(checker)
? builder(
Expand Down
51 changes: 30 additions & 21 deletions scripts/toDataParser/checkerTransformer/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,37 @@ export function checkerTransformer(
},
};

return A.reduce(
params.transformers,
A.reduceFrom<CheckerTransformerEither>(
E.left("checkerNotSupport", checker),
),
({
element: functionBuilder,
lastValue,
next,
exit,
}) => {
const result = functionBuilder(checker, functionParams);
if (checker.definition.mapImportContextEntries) {
TST.applyMapImportContextEntries(
functionParams.addImport,
checker.definition.mapImportContextEntries,
);
}

if (E.isLeft(result)) {
if (!E.hasInformation(result, "checkerNotSupport")) {
return exit(result);
}
return checker.definition.overrideCheckerTransformer
? checker.definition.overrideCheckerTransformer(checker, functionParams)
: A.reduce(
params.transformers,
A.reduceFrom<CheckerTransformerEither>(
E.left("checkerNotSupport", checker),
),
({
element: functionBuilder,
lastValue,
next,
exit,
}) => {
const result = functionBuilder(checker, functionParams);

return next(lastValue);
}
if (E.isLeft(result)) {
if (!E.hasInformation(result, "checkerNotSupport")) {
return exit(result);
}

return exit(result);
},
);
return next(lastValue);
}

return exit(result);
},
);
}
5 changes: 4 additions & 1 deletion scripts/toDataParser/dataParserTransformer/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export type ToTypescriptDataParserNotSupportedEither = E.Left<"toTypescriptDataP

export type ToTypescriptDataParserErrorEither = E.Left<"toTypescriptBuildDataParserError", DP.DataParser>;

export type ToTypescriptCheckerErrorEither = E.Left<"toTypescriptBuildCheckerError", DP.DataParserChecker>;

export type DataParserGetDefinitionErrorEither = E.Left<
"buildDataParserGetDefinitionError",
{
Expand All @@ -38,7 +40,8 @@ export type MaybeTransformerEither =
| DataParserErrorEither
| DataParserGetDefinitionErrorEither
| ToTypescriptDataParserNotSupportedEither
| ToTypescriptDataParserErrorEither;
| ToTypescriptDataParserErrorEither
| ToTypescriptCheckerErrorEither;

export interface TransformerParams {
readonly dependencyIdentifier: Identifier;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { DP } from "@duplojs/utils";
import { createTransformer } from "../create";

export const errorHandlerTransformer = createTransformer(
DP.errorHandlerKind.has,
(
dataParser,
{
transformer,
},
) => transformer(dataParser.definition.inner),
);
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export * from "./transform";
export * from "./tuple";
export * from "./union";
export * from "./unknown";
export * from "./errorHandler";

import type { createTransformer } from "../create";
import { arrayTransformer } from "./array";
Expand All @@ -46,6 +47,7 @@ import { transformTransformer } from "./transform";
import { tupleTransformer } from "./tuple";
import { unionTransformer } from "./union";
import { unknownTransformer } from "./unknown";
import { errorHandlerTransformer } from "./errorHandler";

export const defaultTransformers = [
arrayTransformer,
Expand All @@ -71,4 +73,5 @@ export const defaultTransformers = [
tupleTransformer,
unionTransformer,
unknownTransformer,
errorHandlerTransformer,
] as const satisfies readonly ReturnType<typeof createTransformer>[];
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { A, type DP, E, pipe, when } from "@duplojs/utils";
import { checkerTransformer, type createCheckerTransformer } from "../checkerTransformer";
import { type CallExpression, factory, type PropertyAssignment } from "typescript";
import { type CallExpression, factory, type Identifier, type PropertyAssignment } from "typescript";
import type * as TST from "@scripts/toTypescript";

export interface getDefinitionDataParserParams {
Expand All @@ -27,7 +27,7 @@ export function getDefinitionDataParser(params: getDefinitionDataParserParams) {
if (A.minElements(params.dataParser.definition.checkers, 1)) {
const checkers = A.reduce(
params.dataParser.definition.checkers,
A.reduceFrom<CallExpression[]>([]),
A.reduceFrom<(CallExpression | Identifier)[]>([]),
({ element, lastValue, nextPush, exit }) => pipe(
checkerTransformer(
element,
Expand Down
26 changes: 23 additions & 3 deletions scripts/toDataParser/dataParserTransformer/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface TransformerFunctionParams {
readonly dataParserTransformers: readonly ReturnType<typeof createTransformer>[];
readonly checkerTransformers: readonly ReturnType<typeof createCheckerTransformer>[];
readonly typescriptTransformers: readonly ReturnType<typeof TST.createTransformer>[];
readonly typescriptCheckerRefiner?: readonly ReturnType<typeof TST.createCheckerRefiner>[];
readonly context: MapContext;
readonly typescriptContext: TST.MapContext;
readonly importContext: TST.MapImportContext;
Expand Down Expand Up @@ -122,6 +123,13 @@ export function transformer(
addImport: TST.createAddImport(params.importContext),
};

if (currentDataParser.definition.mapImportContextEntries) {
TST.applyMapImportContextEntries(
functionParams.addImport,
currentDataParser.definition.mapImportContextEntries,
);
}

const result = currentDataParser.definition.overrideDataParserTransformer
? currentDataParser.definition.overrideDataParserTransformer(
currentDataParser.addOverrideDataParserTransformer(null),
Expand All @@ -141,7 +149,14 @@ export function transformer(
const result = functionBuilder(currentDataParser, functionParams);

if (E.isLeft(result)) {
if (!E.hasInformation(result, "dataParserNotSupport")) {
if (
E.hasInformation(result, [
"buildDataParserError",
"buildDataParserGetDefinitionError",
"toTypescriptBuildDataParserError",
"toTypescriptBuildCheckerError",
])
) {
return exit(result);
}

Expand All @@ -168,12 +183,13 @@ export function transformer(
{
identifier,
transformers: params.typescriptTransformers,
checkerRefiner: params.typescriptCheckerRefiner,
context: params.typescriptContext,
importContext: params.importContext,
...params.toTypescript,
},
);
return E.matchInformationOtherwise(
return E.matchInformation(
result,
{
buildDataParserError: (dataParser) => E.left(
Expand All @@ -184,8 +200,12 @@ export function transformer(
"toTypescriptDataParserNotSupport",
dataParser,
),
buildCheckerError: (checker) => E.left(
"toTypescriptBuildCheckerError",
checker,
),
success: () => factory.createIdentifier(identifier),
},
() => factory.createIdentifier(identifier),
);
});

Expand Down
Loading
Loading