Skip to content

Close #815 - new module http - query, headeres and parameter #819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Sep 26, 2023
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Typia is a transformer library supporting below features:
## Sponsors
Thanks for your support.

Your donation would encourage `typia` development.
Your donation encourages `typia` development.

[![Sponsers](https://opencollective.com/typia/badge.svg?avatarHeight=75&width=600)](https://opencollective.com/typia)

Expand Down
70 changes: 70 additions & 0 deletions build/internal/TestFeature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ export interface TestFeature {
method: string;
creatable: boolean;
spoilable: boolean;
query?: true;
headers?: true;
jsonable?: true;
primitive?: true;
resolved?: true;
Expand Down Expand Up @@ -267,6 +269,74 @@ export namespace TestFeature {
jsonable: true,
},

//----
// HTTP
//----
{
module: "http",
method: "query",
creatable: true,
query: true,
resolved: true,
spoilable: false,
},
{
module: "http",
method: "assertQuery",
creatable: true,
query: true,
resolved: true,
spoilable: true,
},
{
module: "http",
method: "isQuery",
creatable: true,
query: true,
resolved: true,
spoilable: true,
},
{
module: "http",
method: "validateQuery",
creatable: true,
query: true,
resolved: true,
spoilable: true,
},
{
module: "http",
method: "headers",
creatable: true,
headers: true,
resolved: true,
spoilable: false,
},
{
module: "http",
method: "assertHeaders",
creatable: true,
headers: true,
resolved: true,
spoilable: true,
},
{
module: "http",
method: "isHeaders",
creatable: true,
headers: true,
resolved: true,
spoilable: true,
},
{
module: "http",
method: "validateHeaders",
creatable: true,
headers: true,
resolved: true,
spoilable: true,
},

//----
// MISCELLANEOUS
//----
Expand Down
2 changes: 2 additions & 0 deletions build/internal/TestStructure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export interface TestStructure<T> {
SPOILERS?: Spoiler<T>[];
ADDABLE?: boolean;
BINARABLE?: boolean;
QUERY?: boolean;
HEADERS?: boolean;
RESOLVABLE?: boolean;
JSONABLE?: boolean;
PRIMITIVE?: boolean;
Expand Down
11 changes: 8 additions & 3 deletions build/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ async function generate(
else if (feat.jsonable && s.JSONABLE === false) continue;
else if (feat.strict && s.ADDABLE === false) continue;
else if (feat.module === "protobuf" && s.BINARABLE === false) continue;
else if (feat.query === true && s.QUERY !== true) continue;
else if (feat.headers === true && s.HEADERS !== true) continue;
else if (feat.primitive && s.PRIMITIVE === false) continue;
else if (feat.resolved && s.RESOLVABLE === false) continue;
else if (
Expand All @@ -79,11 +81,14 @@ function script(
struct: TestStructure<any>,
create: boolean,
): string {
const prefix: string = `test_${feat.module ? `${feat.module}_` : ""}${
const common: string = `_test_${feat.module ? `${feat.module}_` : ""}${
feat.method
}`;
const common: string = `_${prefix}`;
const functor: string = `${prefix}_${struct.name}`;
const functor: string = `test_${feat.module ? `${feat.module}_` : ""}${
create
? `create${feat.method[0].toUpperCase()}${feat.method.slice(1)}`
: feat.method
}_${struct.name}`;
const symbol: string = [
"typia",
...(feat.module ? [feat.module] : []),
Expand Down
2 changes: 2 additions & 0 deletions errors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const validate = (directory) => {
if (x !== y) {
console.error(
`Bug on ${directory}/${file}, succeeded to transform invalid code.`,
x,
y,
);
return false;
}
Expand Down
43 changes: 43 additions & 0 deletions errors/src/http/error_http_headers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import typia from "typia";

interface IPointer<T> {
value: T;
}
type IArray<Key extends string> = {
[P in Key]: string[];
};
interface ISomething {
something: boolean;
sOmething: bigint;
soMething: number;
somEthing: string;
}

typia.http.createHeaders<ISomething>();
typia.http.createHeaders<IPointer<number | string>>();
typia.http.createHeaders<IPointer<number[][]>>();
typia.http.createHeaders<IPointer<[string, string]>>();
typia.http.createHeaders<IPointer<number | null>>();
typia.http.createHeaders<IPointer<IPointer<number>>>();

typia.http.createHeaders<{
"set-cookie": string;
}>();
typia.http.createHeaders<IArray<"age">>();
typia.http.createHeaders<IArray<"authorization">>();
typia.http.createHeaders<IArray<"content-length">>();
typia.http.createHeaders<IArray<"content-type">>();
typia.http.createHeaders<IArray<"etag">>();
typia.http.createHeaders<IArray<"expires">>();
typia.http.createHeaders<IArray<"from">>();
typia.http.createHeaders<IArray<"host">>();
typia.http.createHeaders<IArray<"if-modified-since">>();
typia.http.createHeaders<IArray<"if-unmodified-since">>();
typia.http.createHeaders<IArray<"last-modified">>();
typia.http.createHeaders<IArray<"location">>();
typia.http.createHeaders<IArray<"max-forwards">>();
typia.http.createHeaders<IArray<"proxy-authorization">>();
typia.http.createHeaders<IArray<"referer">>();
typia.http.createHeaders<IArray<"retry-after">>();
typia.http.createHeaders<IArray<"server">>();
typia.http.createHeaders<IArray<"user-agent">>();
5 changes: 5 additions & 0 deletions errors/src/http/error_http_parameter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import typia from "typia";

typia.http.createParameter<any>();
typia.http.createParameter<string | number>();
typia.http.createParameter<1 | 2 | 3n>();
15 changes: 15 additions & 0 deletions errors/src/http/error_http_query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import typia from "typia";

interface IPointer<T> {
value: T;
}
type IArray<Key extends string> = {
[P in Key]: string[];
};

typia.http.createQuery<IPointer<number | string>>();
typia.http.createQuery<IPointer<number[] | undefined>>();
typia.http.createQuery<IPointer<number[][]>>();
typia.http.createQuery<IPointer<[string, string]>>();
typia.http.createQuery<IPointer<number | undefined>>();
typia.http.createQuery<IPointer<IPointer<number>>>();
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typia",
"version": "5.0.5",
"version": "5.1.1",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript-json/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Typia is a transformer library supporting below features:
## Sponsors
Thanks for your support.

Your donation would encourage `typia` development.
Your donation encourages `typia` development.

[![Sponsers](https://opencollective.com/typia/badge.svg?avatarHeight=75&width=600)](https://opencollective.com/typia)

Expand Down
4 changes: 2 additions & 2 deletions packages/typescript-json/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typescript-json",
"version": "5.0.5",
"version": "5.1.1",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -72,7 +72,7 @@
},
"homepage": "https://typia.io",
"dependencies": {
"typia": "5.0.5"
"typia": "5.1.1"
},
"peerDependencies": {
"typescript": ">= 4.8.0"
Expand Down
5 changes: 5 additions & 0 deletions src/factories/MetadataCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ export namespace MetadataCollection {
}

export const replace = (str: string): string => {
let replaced: string = str;
for (const [before] of REPLACERS)
replaced = replaced.split(before).join("");
if (replaced.length !== 0) return replaced;

for (const [before, after] of REPLACERS)
str = str.split(before).join(after);
return str;
Expand Down
24 changes: 24 additions & 0 deletions src/factories/MetadataFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,30 @@ export namespace MetadataFactory {
if (visitor.objects.has(object)) return;
visitor.objects.add(object);

if (options.validate) {
const explore: IExplore = {
object,
top: false,
property: null,
nested: null,
escaped: false,
aliased: false,
};
const errors: string[] = options.validate(
Metadata.create({
...Metadata.initialize(),
objects: [object],
}),
explore,
);
if (errors.length)
visitor.errors.push({
name: object.name,
explore,
messages: [...new Set(errors)],
});
}

for (const property of object.properties)
validateMeta(options)(visitor)(property.value, {
object,
Expand Down
28 changes: 28 additions & 0 deletions src/functional/$HeadersReader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export namespace $HeadersReader {
export const boolean = (value: string | undefined) =>
value !== undefined
? value === "true"
? true
: value === "false"
? false
: value
: undefined;
export const bigint = (value: string | undefined) =>
value !== undefined ? toBigint(value) : undefined;
export const number = (value: string | undefined) =>
value !== undefined ? toNumber(value) : undefined;
export const string = (value: string | undefined) => value;
}

const toNumber = (str: string): number | string => {
const value: number = Number(str);
return isNaN(value) ? str : value;
};

const toBigint = (str: string): bigint | string => {
try {
return BigInt(str);
} catch {
return str;
}
};
31 changes: 31 additions & 0 deletions src/functional/$ParameterReader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export namespace $ParameterReader {
export const boolean = (value: string) =>
value !== "null"
? value === "true" || value === "1"
? true
: value === "false" || value === "0"
? false
: value
: null;

export const bigint = (value: string) =>
value !== "null" ? toBigint(value) : null;

export const number = (value: string) =>
value !== "null" ? toNumber(value) : null;

export const string = (value: string) => (value !== "null" ? value : null);
}

const toNumber = (str: string): number | string => {
const value: number = Number(str);
return isNaN(value) ? str : value;
};

const toBigint = (str: string): bigint | string => {
try {
return BigInt(str);
} catch {
return str;
}
};
56 changes: 56 additions & 0 deletions src/functional/$QueryReader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
export namespace $QueryReader {
export const boolean = (str: string | null): boolean | null | undefined =>
str === null
? undefined
: str === "null"
? null
: str.length === 0
? true
: str === "true" || str === "1"
? true
: str === "false" || str === "0"
? false
: (str as any); // wrong type

export const number = (str: string | null): number | null | undefined =>
!!str?.length
? str === "null"
? null
: (toNumber(str) as any)
: undefined;

export const bigint = (str: string | null): bigint | null | undefined =>
!!str?.length
? str === "null"
? null
: (toBigint(str) as any)
: undefined;

export const string = (str: string | null): string | null | undefined =>
str === null ? undefined : str === "null" ? null : str;

export const params = (input: string | URLSearchParams) => {
if (typeof input === "string") {
const index: number = input.indexOf("?");
input = index === -1 ? "" : input.substring(index + 1);
return new URLSearchParams(input);
}
return input;
};

export const array = (input: any[], alternative: null | undefined) =>
input.length ? input : alternative;
}

const toNumber = (str: string): number | string => {
const value: number = Number(str);
return isNaN(value) ? str : value;
};

const toBigint = (str: string): bigint | string => {
try {
return BigInt(str);
} catch {
return str;
}
};
Loading