Skip to content

Commit

Permalink
feat(zod): add validations variables
Browse files Browse the repository at this point in the history
  • Loading branch information
anymaniax committed Mar 5, 2023
1 parent d6f7f85 commit 15fc8e6
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 533 deletions.
476 changes: 0 additions & 476 deletions docs/generated/endpoints.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/core/src/getters/array.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SchemaObject } from 'openapi3-ts';
import { ContextSpecs, ResolverValue } from '../types';
import { ContextSpecs, ScalarValue } from '../types';
import { resolveObject } from '../resolvers/object';

/**
Expand All @@ -15,7 +15,7 @@ export const getArray = ({
schema: SchemaObject;
name?: string;
context: ContextSpecs;
}): ResolverValue => {
}): ScalarValue => {
if (schema.items) {
const resolvedObject = resolveObject({
schema: schema.items,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/getters/body.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ReferenceObject, RequestBodyObject } from 'openapi3-ts';
import { generalJSTypesWithArray } from '../constants';
import { ContextSpecs, OverrideOutputContentType } from '../types';
import { GetterBody } from '../types';
import { ContextSpecs, GetterBody, OverrideOutputContentType } from '../types';
import { camel } from '../utils';
import { getResReqTypes } from './res-req-types';

Expand Down Expand Up @@ -51,6 +50,7 @@ export const getBody = ({
: camel(definition);

return {
originalSchema: requestBody,
definition,
implementation,
imports,
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/getters/combine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ContextSpecs,
GeneratorImport,
GeneratorSchema,
ResolverValue,
ScalarValue,
SchemaType,
} from '../types';
import { getNumberWord, pascal } from '../utils';
Expand All @@ -30,7 +30,7 @@ const combineValues = ({
separator,
}: {
resolvedData: CombinedData;
resolvedValue?: ResolverValue;
resolvedValue?: ScalarValue;
separator: Separator;
}) => {
const isAllEnums = resolvedData.isEnum.every((v) => v);
Expand Down Expand Up @@ -68,7 +68,7 @@ export const combineSchemas = ({
separator: Separator;
context: ContextSpecs;
nullable: string;
}): ResolverValue => {
}): ScalarValue => {
const items = schema[separator] ?? [];

const resolvedData = items.reduce<CombinedData>(
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/getters/object.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ReferenceObject, SchemaObject } from 'openapi3-ts';
import { resolveObject, resolveValue } from '../resolvers';
import { ContextSpecs, ResolverValue, SchemaType } from '../types';
import { ContextSpecs, ScalarValue, SchemaType } from '../types';
import { isBoolean, isReference, jsDoc, pascal } from '../utils';
import { combineSchemas } from './combine';
import { getKey } from './keys';
Expand All @@ -21,7 +21,7 @@ export const getObject = ({
name?: string;
context: ContextSpecs;
nullable: string;
}): ResolverValue => {
}): ScalarValue => {
if (isReference(item)) {
const { name, specKey } = getRefInfo(item.$ref, context);
return {
Expand Down Expand Up @@ -137,7 +137,7 @@ export const getObject = ({
type: 'object' as SchemaType,
isRef: false,
schema: {},
} as ResolverValue,
} as ScalarValue,
);
}

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/getters/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export const getParams = ({
default: resolvedValue.originalSchema!.default,
required,
imports: resolvedValue.imports,
originalSchema: resolvedValue.originalSchema,
};
});
};
4 changes: 4 additions & 0 deletions packages/core/src/getters/query-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type QueryParamsType = {
definition: string;
imports: GeneratorImport[];
schemas: GeneratorSchema[];
originalSchema: SchemaObject;
};

const getQueryParamsTypes = (
Expand Down Expand Up @@ -60,6 +61,7 @@ const getQueryParamsTypes = (
}`,
imports: parameterImports,
schemas: [],
originalSchema: resolvedeValue.originalSchema,
};
}

Expand All @@ -81,6 +83,7 @@ const getQueryParamsTypes = (
...resolvedeValue.schemas,
{ name: enumName, model: enumValue, imports: resolvedeValue.imports },
],
originalSchema: resolvedeValue.originalSchema,
};
}

Expand All @@ -92,6 +95,7 @@ const getQueryParamsTypes = (
definition,
imports: resolvedeValue.imports,
schemas: resolvedeValue.schemas,
originalSchema: resolvedeValue.originalSchema,
};
});
};
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/getters/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,6 @@ export const getResponse = ({
types: groupedByStatus,
contentTypes,
schemas,
originalSchema: responses,
};
};
4 changes: 2 additions & 2 deletions packages/core/src/getters/scalar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SchemaObject } from 'openapi3-ts';
import { ContextSpecs, ResolverValue } from '../types';
import { ContextSpecs, ScalarValue } from '../types';
import { escape, isString } from '../utils';
import { getArray } from './array';
import { getObject } from './object';
Expand All @@ -18,7 +18,7 @@ export const getScalar = ({
item: SchemaObject;
name?: string;
context: ContextSpecs;
}): ResolverValue => {
}): ScalarValue => {
const nullable = item.nullable ? ' | null' : '';

if (!item.type && item.items) {
Expand Down
17 changes: 14 additions & 3 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import {
OpenAPIObject,
OperationObject,
ParameterObject,
ReferenceObject,
RequestBodyObject,
ResponsesObject,
SchemaObject,
} from 'openapi3-ts';
import swagger2openapi from 'swagger2openapi';
Expand Down Expand Up @@ -592,9 +595,12 @@ export type GetterResponse = {
};
contentTypes: string[];
schemas: GeneratorSchema[];

originalSchema?: ResponsesObject;
};

export type GetterBody = {
originalSchema: ReferenceObject | RequestBodyObject;
imports: GeneratorImport[];
definition: string;
implementation: string;
Expand Down Expand Up @@ -624,6 +630,7 @@ export type GetterQueryParam = {
schema: GeneratorSchema;
deps: GeneratorSchema[];
isOptional: boolean;
originalSchema?: SchemaObject;
};

export type GetterPropType = 'param' | 'body' | 'queryParam' | 'header';
Expand Down Expand Up @@ -669,22 +676,26 @@ export const SchemaType = {
unknown: 'unknown',
};

export type ResolverValue = {
export type ScalarValue = {
value: string;
isEnum: boolean;
type: SchemaType;
imports: GeneratorImport[];
schemas: GeneratorSchema[];
originalSchema?: SchemaObject;
isRef: boolean;
};

export type ResReqTypesValue = ResolverValue & {
export type ResolverValue = ScalarValue & {
originalSchema: SchemaObject;
};

export type ResReqTypesValue = ScalarValue & {
formData?: string;
formUrlEncoded?: string;
isRef?: boolean;
key: string;
contentType: string;
originalSchema?: SchemaObject;
};

export type WriteSpecsBuilder = {
Expand Down
1 change: 1 addition & 0 deletions packages/query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,7 @@ const generateQueryHook = async (
}${body.implementation ? `, ${body.implementation}` : ''}];`;

const implementation = `${!queryKeyMutator ? queryKeyFn : ''}
${queries.reduce(
(acc, queryOption) =>
Expand Down
Loading

0 comments on commit 15fc8e6

Please sign in to comment.