Skip to content

Commit

Permalink
fix: fix unknown body parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
raveclassic committed Nov 7, 2018
1 parent 109c391 commit 998debb
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions src/language/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,6 @@ const serializedType = (type: string, io: string, dependencies: TDepdendency[],
refs,
});

const SERIALIZED_MIXED_DICTIONARY_TYPE = serializedType(
'Record<string, t.mixed>',
't.Dictionary',
EMPTY_DEPENDENCIES,
EMPTY_REFS,
);

type TSerializedParameter = TSerializedType & {
isRequired: boolean;
};
Expand Down Expand Up @@ -138,15 +131,19 @@ const CONTROLLERS_DIRECTORY = 'controllers';
const DEFINITIONS_DIRECTORY = 'definitions';
const CLIENT_DIRECTORY = 'client';
const CLIENT_FILENAME = 'client';
const UTILS_DIRECTORY = 'utils';
const UTILS_FILENAME = 'utils';

const getRelativeRoot = (cwd: string) => path.relative(cwd, ROOT_DIRECTORY);
const getRelativeDefinitionPath = (cwd: string, definitionFileName: string): string =>
`${getRelativeRoot(cwd)}/${DEFINITIONS_DIRECTORY}/${definitionFileName}`;
const getRelativeClientPath = (cwd: string): string => `${getRelativeRoot(cwd)}/${CLIENT_DIRECTORY}/${CLIENT_FILENAME}`;
const getRelativeUtilsPath = (cwd: string): string => `${getRelativeRoot(cwd)}/${UTILS_DIRECTORY}/${UTILS_FILENAME}`;

export const serialize: TSerializer = (name: string, swaggerObject: TSwaggerObject): TDirectory =>
directory(name, [
directory(CLIENT_DIRECTORY, [file(`${CLIENT_FILENAME}.ts`, client)]),
directory(UTILS_DIRECTORY, [file(`${UTILS_FILENAME}.ts`, utils)]),
...catOptions([swaggerObject.definitions.map(serializeDefinitions)]),
serializePaths(swaggerObject.paths),
]);
Expand Down Expand Up @@ -295,7 +292,14 @@ const serializeSchemaObject = (schema: TSchemaObject, rootName: string, cwd: str
return toObjectType(serialized, serialized.refs.includes(rootName) ? some(rootName) : none);
}),
)
.getOrElse(SERIALIZED_MIXED_DICTIONARY_TYPE);
.getOrElseL(() =>
serializedType(
'unknown',
'unknownType',
[dependency('unknownType', getRelativeUtilsPath(cwd))],
EMPTY_REFS,
),
);
}
}
};
Expand Down Expand Up @@ -584,7 +588,7 @@ const client = `
export type TAPIRequest = {
url: string;
query?: object;
body?: object;
body?: unknown;
};
export type TFullAPIRequest = TAPIRequest & {
Expand All @@ -607,6 +611,17 @@ const client = `
}
`;

const utils = `
import * as t from 'io-ts';
export const unknownType = new class UnknownType extends t.Type<unknown> {
readonly _tag: 'UnknownType' = 'UnknownType';
constructor() {
super('unknownType', (_: unknown): _ is unknown => true, t.success, t.identity);
}
}();
`;

const hasRequiredParameters = (parameters: Array<TQueryParameterObject | TBodyParameterObject>): boolean =>
parameters.some(p => p.required.exists(identity));

Expand Down

0 comments on commit 998debb

Please sign in to comment.