Skip to content

Commit

Permalink
fix: gen schema should avoid to touch typescript infer limit
Browse files Browse the repository at this point in the history
  • Loading branch information
morlay committed Aug 8, 2024
1 parent a671267 commit ffd5bdf
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 95 deletions.
2 changes: 1 addition & 1 deletion nodepkg/gents/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@innoai-tech/gents",
"version": "0.6.9",
"version": "0.6.12",
"monobundle": {
"build": {
"clean": true
Expand Down
51 changes: 26 additions & 25 deletions nodepkg/gents/src/ClientGen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import {
keys,
lowerFirst,
set,
values,
values
} from "@innoai-tech/lodash";
import {
type AnyType,
JSONSchemaDecoder,
TypeScriptEncoder,
TypedefEncoder,
refName,
t,
t
} from "@innoai-tech/typedef";
import { Genfile, dumpObj } from "./Genfile";

Expand All @@ -26,15 +26,15 @@ export class ClientGen extends Genfile {
constructor(
private clientID: string,
private openapi: any,
private requestCreator: RequestCreator,
private requestCreator: RequestCreator
) {
super();
}

typedef = new JSONSchemaDecoder((ref) => {
return [
get(this.openapi, (ref.split("#/")[1] ?? "").split("/")),
refName(ref),
refName(ref)
];
});

Expand All @@ -50,6 +50,7 @@ export class ClientGen extends Genfile {
scan() {
this.import(this.requestCreator.importPath, this.requestCreator.expose, "");
this.import("@innoai-tech/typedef", "t", "");
this.import("@innoai-tech/typedef", "type OptionalType", "");

const operations: Record<string, { method: string; path: string } & any> =
{};
Expand All @@ -71,7 +72,7 @@ export class ClientGen extends Genfile {
operations[op.operationId] = {
...op,
method: method,
path: path,
path: path
};
}
}
Expand All @@ -86,7 +87,7 @@ export class ClientGen extends Genfile {
scanOperation(method: string, path: string, op: any) {
const requestObject = {
method: method.toUpperCase(),
url: path,
url: path
};

const requestParameterSchema: Record<string, AnyType> = {};
Expand All @@ -111,30 +112,30 @@ export class ClientGen extends Genfile {
hasParamInPath = true;
requestObject.url = requestObject.url.replace(
`{${p.name}}`,
`\${${p.in}_${lowerCamelCase(p.name)}}`,
`\${${p.in}_${lowerCamelCase(p.name)}}`
);
}

if (p.in === "header") {
set(
requestObject,
["headers", p.name],
Genfile.id(`${p.in}_${lowerCamelCase(p.name)}`),
Genfile.id(`${p.in}_${lowerCamelCase(p.name)}`)
);
}

if (p.in === "query") {
set(
requestObject,
["params", p.name],
Genfile.id(`${p.in}_${lowerCamelCase(p.name)}`),
Genfile.id(`${p.in}_${lowerCamelCase(p.name)}`)
);
}

set(
requestUsed,
p.name,
Genfile.id(`${p.in}_${lowerCamelCase(p.name)}`),
Genfile.id(`${p.in}_${lowerCamelCase(p.name)}`)
);

if (p.required) {
Expand All @@ -143,7 +144,7 @@ export class ClientGen extends Genfile {
set(
requestParameterSchema,
p.name,
this.typedef.decode(p.schema).optional(),
this.typedef.decode(p.schema).optional()
);
}
}
Expand Down Expand Up @@ -177,20 +178,20 @@ export class ClientGen extends Genfile {
set(
requestUsed,
"Content-Type",
Genfile.id(`${lowerCamelCase("Content-Type")}`),
Genfile.id(`${lowerCamelCase("Content-Type")}`)
);

set(
requestObject,
["headers", "Content-Type"],
Genfile.id(`${lowerCamelCase("Content-Type")}`),
Genfile.id(`${lowerCamelCase("Content-Type")}`)
);

bodyTypes.push(
t.object({
"Content-Type": t.literal(ct),
body: this.typedef.decode(schema),
}),
body: this.typedef.decode(schema)
})
);
}
}
Expand All @@ -202,25 +203,25 @@ export class ClientGen extends Genfile {
const requestType = isRequestTypeEmpty
? "void"
: this.decodeAsTypeScript(
bodyTypes.length > 0
? t.intersection(
t.object(requestParameterSchema),
t.union(...bodyTypes),
)
: t.object(requestParameterSchema),
);
bodyTypes.length > 0
? t.intersection(
t.object(requestParameterSchema),
t.union(...bodyTypes)
)
: t.object(requestParameterSchema)
);

const responseType = this.decodeAsTypeScript(
this.typedef.decode(getRespBodySchema(op.responses)),
this.typedef.decode(getRespBodySchema(op.responses))
);

this.decl(`
export const ${lowerCamelCase(op.operationId)} =
/*#__PURE__*/${this.requestCreator.expose}<${requestType}, ${responseType}>(
"${this.clientID}.${op.operationId}",
(${isRequestTypeEmpty ? "" : dumpObj(requestUsed)}) => (${dumpObj(
requestObject,
)}),
requestObject
)}),
)
`);
}
Expand Down
32 changes: 16 additions & 16 deletions nodepkg/gents/src/__tests__/client/example.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createRequest } from "./client";

import { t } from "@innoai-tech/typedef";
import { t, type OptionalType } from "@innoai-tech/typedef";


export const applyKubePkg =
Expand Down Expand Up @@ -214,10 +214,10 @@ export type KubepkgV1Alpha1Statuses = { [k: string]: any }


export const KubepkgV1Alpha1KubePkgSchema = /*#__PURE__*/t.intersection(t.ref<typeof MetaV1TypeMetaSchema>("MetaV1TypeMeta", () => MetaV1TypeMetaSchema), t.object({
"metadata": t.ref<typeof MetaV1ObjectMetaSchema>("MetaV1ObjectMeta", () => MetaV1ObjectMetaSchema).optional(),
"spec": t.ref<typeof KubepkgV1Alpha1KubePkgSpecSchema>("KubepkgV1Alpha1KubePkgSpec", () => KubepkgV1Alpha1KubePkgSpecSchema).optional(),
"status": t.ref<typeof KubepkgV1Alpha1KubePkgStatusSchema>("KubepkgV1Alpha1KubePkgStatus", () => KubepkgV1Alpha1KubePkgStatusSchema).optional(),
})).use(t.annotate({ description: "KubePkg" }))
"metadata": t.ref<typeof MetaV1ObjectMetaSchema>("MetaV1ObjectMeta", () => MetaV1ObjectMetaSchema).optional() as unknown as OptionalType<typeof MetaV1ObjectMetaSchema>,
"spec": t.ref<typeof KubepkgV1Alpha1KubePkgSpecSchema>("KubepkgV1Alpha1KubePkgSpec", () => KubepkgV1Alpha1KubePkgSpecSchema).optional() as unknown as OptionalType<typeof KubepkgV1Alpha1KubePkgSpecSchema>,
"status": t.ref<typeof KubepkgV1Alpha1KubePkgStatusSchema>("KubepkgV1Alpha1KubePkgStatus", () => KubepkgV1Alpha1KubePkgStatusSchema).optional() as unknown as OptionalType<typeof KubepkgV1Alpha1KubePkgStatusSchema>,
})).annotate({ description: "KubePkg" })

export const MetaV1TypeMetaSchema = /*#__PURE__*/t.object({
"apiVersion": t.string().optional(),
Expand All @@ -227,9 +227,9 @@ export const MetaV1TypeMetaSchema = /*#__PURE__*/t.object({
export const MetaV1ObjectMetaSchema = /*#__PURE__*/t.object({
"annotations": t.record(t.string(), t.string()).optional(),
"clusterName": t.string().optional(),
"creationTimestamp": t.ref<typeof MetaV1TimeSchema>("MetaV1Time", () => MetaV1TimeSchema).optional(),
"creationTimestamp": t.ref<typeof MetaV1TimeSchema>("MetaV1Time", () => MetaV1TimeSchema).optional() as unknown as OptionalType<typeof MetaV1TimeSchema>,
"deletionGracePeriodSeconds": t.integer().optional(),
"deletionTimestamp": t.ref<typeof MetaV1TimeSchema>("MetaV1Time", () => MetaV1TimeSchema).optional(),
"deletionTimestamp": t.ref<typeof MetaV1TimeSchema>("MetaV1Time", () => MetaV1TimeSchema).optional() as unknown as OptionalType<typeof MetaV1TimeSchema>,
"finalizers": t.array(t.string()).optional(),
"generateName": t.string().optional(),
"generation": t.integer().optional(),
Expand All @@ -240,19 +240,19 @@ export const MetaV1ObjectMetaSchema = /*#__PURE__*/t.object({
"ownerReferences": t.array(t.ref<typeof MetaV1OwnerReferenceSchema>("MetaV1OwnerReference", () => MetaV1OwnerReferenceSchema)).optional(),
"resourceVersion": t.string().optional(),
"selfLink": t.string().optional(),
"uid": t.ref<typeof TypesUidSchema>("TypesUid", () => TypesUidSchema).optional(),
"uid": t.ref<typeof TypesUidSchema>("TypesUid", () => TypesUidSchema).optional() as unknown as OptionalType<typeof TypesUidSchema>,
})

export const MetaV1TimeSchema = /*#__PURE__*/t.string()

export const MetaV1ManagedFieldsEntrySchema = /*#__PURE__*/t.object({
"apiVersion": t.string().optional(),
"fieldsType": t.string().optional(),
"fieldsV1": t.ref<typeof MetaV1FieldsV1Schema>("MetaV1FieldsV1", () => MetaV1FieldsV1Schema).optional(),
"fieldsV1": t.ref<typeof MetaV1FieldsV1Schema>("MetaV1FieldsV1", () => MetaV1FieldsV1Schema).optional() as unknown as OptionalType<typeof MetaV1FieldsV1Schema>,
"manager": t.string().optional(),
"operation": t.ref<typeof MetaV1ManagedFieldsOperationTypeSchema>("MetaV1ManagedFieldsOperationType", () => MetaV1ManagedFieldsOperationTypeSchema).optional(),
"operation": t.ref<typeof MetaV1ManagedFieldsOperationTypeSchema>("MetaV1ManagedFieldsOperationType", () => MetaV1ManagedFieldsOperationTypeSchema).optional() as unknown as OptionalType<typeof MetaV1ManagedFieldsOperationTypeSchema>,
"subresource": t.string().optional(),
"time": t.ref<typeof MetaV1TimeSchema>("MetaV1Time", () => MetaV1TimeSchema).optional(),
"time": t.ref<typeof MetaV1TimeSchema>("MetaV1Time", () => MetaV1TimeSchema).optional() as unknown as OptionalType<typeof MetaV1TimeSchema>,
})

export const MetaV1FieldsV1Schema = /*#__PURE__*/t.record(t.string(), t.any())
Expand All @@ -265,31 +265,31 @@ export const MetaV1OwnerReferenceSchema = /*#__PURE__*/t.object({
"controller": t.boolean().optional(),
"kind": t.string(),
"name": t.string(),
"uid": t.ref<typeof TypesUidSchema>("TypesUid", () => TypesUidSchema),
"uid": t.ref<typeof TypesUidSchema>("TypesUid", () => TypesUidSchema) as unknown as typeof TypesUidSchema,
})

export const TypesUidSchema = /*#__PURE__*/t.string()

export const KubepkgV1Alpha1KubePkgSpecSchema = /*#__PURE__*/t.object({
"images": t.record(t.string(), t.string()).optional(),
"manifests": t.ref<typeof KubepkgV1Alpha1ManifestsSchema>("KubepkgV1Alpha1Manifests", () => KubepkgV1Alpha1ManifestsSchema).optional(),
"manifests": t.ref<typeof KubepkgV1Alpha1ManifestsSchema>("KubepkgV1Alpha1Manifests", () => KubepkgV1Alpha1ManifestsSchema).optional() as unknown as OptionalType<typeof KubepkgV1Alpha1ManifestsSchema>,
"version": t.string(),
})

export const KubepkgV1Alpha1ManifestsSchema = /*#__PURE__*/t.record(t.string(), t.any())

export const KubepkgV1Alpha1KubePkgStatusSchema = /*#__PURE__*/t.object({
"digests": t.array(t.ref<typeof KubepkgV1Alpha1DigestMetaSchema>("KubepkgV1Alpha1DigestMeta", () => KubepkgV1Alpha1DigestMetaSchema)).optional(),
"statuses": t.ref<typeof KubepkgV1Alpha1StatusesSchema>("KubepkgV1Alpha1Statuses", () => KubepkgV1Alpha1StatusesSchema).optional(),
"statuses": t.ref<typeof KubepkgV1Alpha1StatusesSchema>("KubepkgV1Alpha1Statuses", () => KubepkgV1Alpha1StatusesSchema).optional() as unknown as OptionalType<typeof KubepkgV1Alpha1StatusesSchema>,
})

export const KubepkgV1Alpha1DigestMetaSchema = /*#__PURE__*/t.object({
"digest": t.string(),
"name": t.string(),
"platform": t.string().optional(),
"size": t.ref<typeof KubepkgV1Alpha1FileSizeSchema>("KubepkgV1Alpha1FileSize", () => KubepkgV1Alpha1FileSizeSchema),
"size": t.ref<typeof KubepkgV1Alpha1FileSizeSchema>("KubepkgV1Alpha1FileSize", () => KubepkgV1Alpha1FileSizeSchema) as unknown as typeof KubepkgV1Alpha1FileSizeSchema,
"tag": t.string().optional(),
"type": t.ref<typeof KubepkgV1Alpha1DigestMetaTypeSchema>("KubepkgV1Alpha1DigestMetaType", () => KubepkgV1Alpha1DigestMetaTypeSchema),
"type": t.ref<typeof KubepkgV1Alpha1DigestMetaTypeSchema>("KubepkgV1Alpha1DigestMetaType", () => KubepkgV1Alpha1DigestMetaTypeSchema) as unknown as typeof KubepkgV1Alpha1DigestMetaTypeSchema,
})

export const KubepkgV1Alpha1FileSizeSchema = /*#__PURE__*/t.integer()
Expand Down
2 changes: 1 addition & 1 deletion nodepkg/typedef/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@innoai-tech/typedef",
"version": "0.2.33",
"version": "0.2.37",
"monobundle": {
"exports": {
".": "./src/index.ts"
Expand Down
Loading

0 comments on commit ffd5bdf

Please sign in to comment.