Skip to content

Commit

Permalink
feat(core): add possibility to generate headers
Browse files Browse the repository at this point in the history
  • Loading branch information
anymaniax committed Jul 6, 2022
1 parent bd97425 commit 6dbaa3f
Show file tree
Hide file tree
Showing 18 changed files with 200 additions and 38 deletions.
18 changes: 16 additions & 2 deletions docs/src/pages/reference/configuration/output.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,25 @@ Default Value: `false`.

Can be used to specify `tslint` ([TSLint is deprecated in favour of eslint + plugins](https://github.com/palantir/tslint#tslint)) as typescript linter instead of `eslint`. You need to have tslint in your dependencies.

### tsconfig
### headers

Type: `Boolean`.

Can be used to specify the path to your `tsconfig`.
Use to enable the generation of the headers

### tsconfig

Type: `String | Tsconfig`.

Should be automatically found and transparent for you.
Can be used to specify the path to your `tsconfig` or directly your config.

### packageJson

Type: `String`.

Should be automatically found and transparent for you.
Can be used to specify the path to your `package.json`.

### override

Expand Down
3 changes: 3 additions & 0 deletions src/core/generators/angular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export const generateAngularFooter = () =>

const generateImplementation = (
{
headers,
queryParams,
operationName,
response,
Expand Down Expand Up @@ -146,6 +147,7 @@ const generateImplementation = (
const mutatorConfig = generateMutatorConfig({
route,
body,
headers,
queryParams,
response,
verb,
Expand Down Expand Up @@ -182,6 +184,7 @@ const generateImplementation = (
const options = generateOptions({
route,
body,
headers,
queryParams,
response,
verb,
Expand Down
5 changes: 4 additions & 1 deletion src/core/generators/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@ export const generateApi = async ({
});

const schemas = verbsOptions.reduce<GeneratorSchema[]>(
(acc, { queryParams, body, response }) => {
(acc, { queryParams, headers, body, response }) => {
if (queryParams) {
acc.push(queryParams.schema, ...queryParams.deps);
}
if (headers) {
acc.push(headers.schema, ...headers.deps);
}

acc.push(...body.schemas);
acc.push(...response.schemas);
Expand Down
3 changes: 3 additions & 0 deletions src/core/generators/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const getAxiosDependencies = (hasGlobalMutator: boolean) => [

const generateAxiosImplementation = (
{
headers,
queryParams,
operationName,
response,
Expand Down Expand Up @@ -78,6 +79,7 @@ const generateAxiosImplementation = (
const mutatorConfig = generateMutatorConfig({
route,
body,
headers,
queryParams,
response,
verb,
Expand Down Expand Up @@ -125,6 +127,7 @@ const generateAxiosImplementation = (
const options = generateOptions({
route,
body,
headers,
queryParams,
response,
verb,
Expand Down
2 changes: 2 additions & 0 deletions src/core/generators/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,12 @@ export const generateVerbImports = ({
response,
body,
queryParams,
headers,
params,
}: GeneratorVerbOptions): GeneratorImport[] => [
...response.imports,
...body.imports,
...(queryParams ? [{ name: queryParams.schema.name }] : []),
...(headers ? [{ name: headers.schema.name }] : []),
...params.flatMap<GeneratorImport>(({ imports }) => imports),
];
34 changes: 29 additions & 5 deletions src/core/generators/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,24 @@ export const generateBodyOptions = (
export const generateAxiosOptions = (
response: GetterResponse,
queryParams?: GeneratorSchema,
headers?: GeneratorSchema,
requestOptions?: object | boolean,
) => {
const isRequestOptions = requestOptions !== false;
if (!queryParams && !response.isBlob) {
if (!queryParams && !headers && !response.isBlob) {
return isRequestOptions ? 'options' : '';
}

let value = '';

if (queryParams) {
value += '\n params,';
if (!isRequestOptions) {
if (queryParams) {
value += '\n params,';
}

if (headers) {
value += '\n headers,';
}
}

if (
Expand All @@ -58,7 +65,15 @@ export const generateAxiosOptions = (
}

if (isRequestOptions) {
value += '\n ...options';
value += '\n ...options,';

if (queryParams) {
value += '\n params: {...params, ...options?.params},';
}

if (headers) {
value += '\n headers: {...headers, ...options?.headers},';
}
}

return value;
Expand All @@ -67,6 +82,7 @@ export const generateAxiosOptions = (
export const generateOptions = ({
route,
body,
headers,
queryParams,
response,
verb,
Expand All @@ -77,6 +93,7 @@ export const generateOptions = ({
}: {
route: string;
body: GetterBody;
headers?: GetterQueryParam;
queryParams?: GetterQueryParam;
response: GetterResponse;
verb: Verbs;
Expand All @@ -93,6 +110,7 @@ export const generateOptions = ({
const axiosOptions = generateAxiosOptions(
response,
queryParams?.schema,
headers?.schema,
requestOptions,
);

Expand Down Expand Up @@ -159,6 +177,7 @@ export const generateQueryParamsAxiosConfig = (
export const generateMutatorConfig = ({
route,
body,
headers,
queryParams,
response,
verb,
Expand All @@ -170,6 +189,7 @@ export const generateMutatorConfig = ({
}: {
route: string;
body: GetterBody;
headers?: GetterQueryParam;
queryParams?: GetterQueryParam;
response: GetterResponse;
verb: Verbs;
Expand All @@ -189,7 +209,11 @@ export const generateMutatorConfig = ({
);

const headerOptions = body.contentType
? `,\n headers: {'Content-Type': '${body.contentType}'}`
? `,\n headers: {'Content-Type': '${body.contentType}', ${
headers ? '...headers' : ''
}}`
: headers
? ',\n headers'
: '';

return `{url: \`${route}\`, method: '${verb}'${headerOptions}${bodyOptions}${queryParamsOptions}${
Expand Down
36 changes: 25 additions & 11 deletions src/core/generators/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export const getVueQueryDependencies = (hasGlobalMutator: boolean) => [

const generateQueryRequestFunction = (
{
headers,
queryParams,
operationName,
response,
Expand Down Expand Up @@ -163,6 +164,7 @@ const generateQueryRequestFunction = (
const mutatorConfig = generateMutatorConfig({
route,
body,
headers,
queryParams,
response,
verb,
Expand Down Expand Up @@ -224,6 +226,7 @@ const generateQueryRequestFunction = (
const options = generateOptions({
route,
body,
headers,
queryParams,
response,
verb,
Expand Down Expand Up @@ -422,9 +425,9 @@ const getHookOptions = ({
const generateQueryImplementation = ({
queryOption: { name, queryParam, options, type },
operationName,
queryProps,
queryKeyFnName,
properties,
queryProperties,
queryKeyProperties,
params,
props,
mutator,
Expand All @@ -441,23 +444,24 @@ const generateQueryImplementation = ({
};
isRequestOptions: boolean;
operationName: string;
queryProps: string;
queryKeyFnName: string;
properties: string;
queryProperties: string;
queryKeyProperties: string;
params: GetterParams;
props: GetterProps;
response: GetterResponse;
mutator?: GeneratorMutator;
outputClient: OutputClient | OutputClientFunc;
isExactOptionalPropertyTypes: boolean;
}) => {
const queryProps = toObjectString(props, 'implementation');
const httpFunctionProps = queryParam
? props
.map(({ name }) =>
name === 'params' ? `{ ${queryParam}: pageParam, ...params }` : name,
)
.join(',')
: properties;
: queryProperties;

const returnType = generateQueryReturnType({
outputClient,
Expand Down Expand Up @@ -511,7 +515,7 @@ export const ${camel(
${hookOptions}
const queryKey = queryOptions?.queryKey ?? ${queryKeyFnName}(${properties});
const queryKey = queryOptions?.queryKey ?? ${queryKeyFnName}(${queryKeyProperties});
${
mutator?.isHook
Expand Down Expand Up @@ -574,7 +578,14 @@ const generateQueryHook = (
operationQueryOptions?.useInfinite ||
operationQueryOptions?.useQuery
) {
const properties = props
const queryProperties = props
.map(({ name, type }) =>
type === GetterPropType.BODY ? body.implementation : name,
)
.join(',');

const queryKeyProperties = props
.filter((prop) => prop.type !== GetterPropType.HEADER)
.map(({ name, type }) =>
type === GetterPropType.BODY ? body.implementation : name,
)
Expand Down Expand Up @@ -603,9 +614,12 @@ const generateQueryHook = (
];

const queryKeyFnName = camel(`get-${operationName}-queryKey`);
const queryProps = toObjectString(props, 'implementation');
const queryKeyProps = toObjectString(
props.filter((prop) => prop.type !== GetterPropType.HEADER),
'implementation',
);

return `export const ${queryKeyFnName} = (${queryProps}) => [\`${route}\`${
return `export const ${queryKeyFnName} = (${queryKeyProps}) => [\`${route}\`${
queryParams ? ', ...(params ? [params]: [])' : ''
}${body.implementation ? `, ${body.implementation}` : ''}];
Expand All @@ -615,9 +629,9 @@ const generateQueryHook = (
generateQueryImplementation({
queryOption,
operationName,
queryProps,
queryKeyFnName,
properties,
queryProperties,
queryKeyProperties,
params,
props,
mutator,
Expand Down
Loading

1 comment on commit 6dbaa3f

@vercel
Copy link

@vercel vercel bot commented on 6dbaa3f Jul 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.