Skip to content

Commit

Permalink
fix: allow overriding generated headers from options
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlubos committed Sep 19, 2024
1 parent c39ee74 commit a756987
Show file tree
Hide file tree
Showing 40 changed files with 221 additions and 148 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-cats-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

fix: allow overriding generated headers from options
40 changes: 20 additions & 20 deletions packages/openapi-ts/src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,22 +418,15 @@ export const createObjectType = <
// Check key value equality before possibly modifying it
let canShorthand = false;
if ('key' in value) {
let { key } = value;
const { key } = value;
canShorthand = key === value.value;
if (
key.match(/^[0-9]/) &&
key.match(/\D+/g) &&
((key.match(/^[0-9]/) && key.match(/\D+/g)) ||
key.match(/\W/g)) &&
!key.startsWith("'") &&
!key.endsWith("'")
) {
key = `'${key}'`;
}
if (
key.match(/\W/g) &&
!key.startsWith("'") &&
!key.endsWith("'")
) {
key = `'${key}'`;
value.key = `'${key}'`;
}
}
let assignment: ObjectAssignment;
Expand All @@ -456,15 +449,22 @@ export const createObjectType = <
} else {
let initializer: ts.Expression | undefined = isTsNode(value.value)
? value.value
: toExpression({
identifiers: identifiers.includes(value.key)
? Object.keys(value.value)
: [],
isValueAccess: value.isValueAccess,
shorthand,
unescape,
value: value.value,
});
: Array.isArray(value.value)
? createObjectType({
multiLine,
obj: value.value,
shorthand,
unescape,
})
: toExpression({
identifiers: identifiers.includes(value.key)
? Object.keys(value.value)

Check warning on line 461 in packages/openapi-ts/src/compiler/types.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/compiler/types.ts#L461

Added line #L461 was not covered by tests
: [],
isValueAccess: value.isValueAccess,
shorthand,
unescape,
value: value.value,
});
if (!initializer) {
return undefined;
}
Expand Down
26 changes: 19 additions & 7 deletions packages/openapi-ts/src/generate/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,16 @@ const toRequestOptions = (
},
{
key: 'headers',
value: {
// no need for Content-Type header, browser will set it automatically
'Content-Type': null,
},
value: [
{
// no need for Content-Type header, browser will set it automatically
key: 'Content-Type',
value: null,
},
{
spread: 'options?.headers',
},
],
},
];
onClientImport?.('formDataBodySerializer');
Expand All @@ -313,9 +319,15 @@ const toRequestOptions = (
},
{
key: 'headers',
value: {
'Content-Type': contents[0],
},
value: [
{
key: 'Content-Type',
value: contents[0],
},
{
spread: 'options?.headers',
},
],
},
];
onClientImport?.('urlSearchParamsBodySerializer');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const EnumWithStrings = {
ERROR: 'Error',
_SINGLE_QUOTE_: "'Single Quote'",
_DOUBLE_QUOTES_: '"Double Quotes"',
NON_ASCII__ØÆÅÔÖ_ØÆÅÔÖ字符串: 'Non-ascii: øæåôöØÆÅÔÖ字符串'
'NON_ASCII__ØÆÅÔÖ_ØÆÅÔÖ字符串': 'Non-ascii: øæåôöØÆÅÔÖ字符串'
} as const;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ export const postApiVbyApiVersionFormData = <ThrowOnError extends boolean = fals
...options,
...formDataBodySerializer,
headers: {
'Content-Type': null
'Content-Type': null,
...options?.headers
},
url: '/api/v{api-version}/formData/'
}); };
Expand Down Expand Up @@ -196,7 +197,8 @@ export const uploadFile = <ThrowOnError extends boolean = false>(options: Option
...options,
...urlSearchParamsBodySerializer,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
'Content-Type': 'application/x-www-form-urlencoded',
...options?.headers
},
url: '/api/v{api-version}/upload'
}); };
Expand All @@ -215,7 +217,8 @@ export const multipartRequest = <ThrowOnError extends boolean = false>(options?:
...options,
...formDataBodySerializer,
headers: {
'Content-Type': null
'Content-Type': null,
...options?.headers
},
url: '/api/v{api-version}/multipart'
}); };
Expand Down Expand Up @@ -252,7 +255,8 @@ export const putWithFormUrlEncoded = <ThrowOnError extends boolean = false>(opti
...options,
...urlSearchParamsBodySerializer,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
'Content-Type': 'application/x-www-form-urlencoded',
...options?.headers
},
url: '/api/v{api-version}/non-ascii-æøåÆØÅöôêÊ字符串'
}); };
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ export const EnumWithStrings = {
ERROR: 'Error',
_SINGLE_QUOTE_: "'Single Quote'",
_DOUBLE_QUOTES_: '"Double Quotes"',
NON_ASCII__ØÆÅÔÖ_ØÆÅÔÖ字符串: 'Non-ascii: øæåôöØÆÅÔÖ字符串'
'NON_ASCII__ØÆÅÔÖ_ØÆÅÔÖ字符串': 'Non-ascii: øæåôöØÆÅÔÖ字符串'
} as const;

export type EnumWithReplacedCharacters = "'Single Quote'" | '"Double Quotes"' | 'øæåôöØÆÅÔÖ字符串' | 3.1 | '';

export const EnumWithReplacedCharacters = {
_SINGLE_QUOTE_: "'Single Quote'",
_DOUBLE_QUOTES_: '"Double Quotes"',
ØÆÅÔÖ_ØÆÅÔÖ字符串: 'øæåôöØÆÅÔÖ字符串',
'ØÆÅÔÖ_ØÆÅÔÖ字符串': 'øæåôöØÆÅÔÖ字符串',
'_3.1': 3.1,
EMPTY_STRING: ''
} as const;
Expand Down Expand Up @@ -361,7 +361,7 @@ export const foo_bar_enum = {
SUCCESS: 'Success',
WARNING: 'Warning',
ERROR: 'Error',
ØÆÅ字符串: 'ØÆÅ字符串'
'ØÆÅ字符串': 'ØÆÅ字符串'
} as const;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ export class FormDataService {
...options,
...formDataBodySerializer,
headers: {
'Content-Type': null
'Content-Type': null,
...options?.headers
},
url: '/api/v{api-version}/formData/'
});
Expand Down Expand Up @@ -346,7 +347,8 @@ export class UploadService {
...options,
...urlSearchParamsBodySerializer,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
'Content-Type': 'application/x-www-form-urlencoded',
...options?.headers
},
url: '/api/v{api-version}/upload'
});
Expand Down Expand Up @@ -387,7 +389,8 @@ export class MultipartService {
...options,
...formDataBodySerializer,
headers: {
'Content-Type': null
'Content-Type': null,
...options?.headers
},
url: '/api/v{api-version}/multipart'
});
Expand Down Expand Up @@ -438,7 +441,8 @@ export class NonAsciiÆøåÆøÅöôêÊService {
...options,
...urlSearchParamsBodySerializer,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
'Content-Type': 'application/x-www-form-urlencoded',
...options?.headers
},
url: '/api/v{api-version}/non-ascii-æøåÆØÅöôêÊ字符串'
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ export const EnumWithStrings = {
ERROR: 'Error',
_SINGLE_QUOTE_: "'Single Quote'",
_DOUBLE_QUOTES_: '"Double Quotes"',
NON_ASCII__ØÆÅÔÖ_ØÆÅÔÖ字符串: 'Non-ascii: øæåôöØÆÅÔÖ字符串'
'NON_ASCII__ØÆÅÔÖ_ØÆÅÔÖ字符串': 'Non-ascii: øæåôöØÆÅÔÖ字符串'
} as const;

export type EnumWithReplacedCharacters = "'Single Quote'" | '"Double Quotes"' | 'øæåôöØÆÅÔÖ字符串' | 3.1 | '';

export const EnumWithReplacedCharacters = {
_SINGLE_QUOTE_: "'Single Quote'",
_DOUBLE_QUOTES_: '"Double Quotes"',
ØÆÅÔÖ_ØÆÅÔÖ字符串: 'øæåôöØÆÅÔÖ字符串',
'ØÆÅÔÖ_ØÆÅÔÖ字符串': 'øæåôöØÆÅÔÖ字符串',
'_3.1': 3.1,
EMPTY_STRING: ''
} as const;
Expand Down Expand Up @@ -361,7 +361,7 @@ export const foo_bar_enum = {
SUCCESS: 'Success',
WARNING: 'Warning',
ERROR: 'Error',
ØÆÅ字符串: 'ØÆÅ字符串'
'ØÆÅ字符串': 'ØÆÅ字符串'
} as const;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ export const postApiVbyApiVersionFormData = <ThrowOnError extends boolean = fals
...options,
...formDataBodySerializer,
headers: {
'Content-Type': null
'Content-Type': null,
...options?.headers
},
url: '/api/v{api-version}/formData/'
}); };
Expand Down Expand Up @@ -196,7 +197,8 @@ export const uploadFile = <ThrowOnError extends boolean = false>(options: Option
...options,
...urlSearchParamsBodySerializer,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
'Content-Type': 'application/x-www-form-urlencoded',
...options?.headers
},
url: '/api/v{api-version}/upload'
}); };
Expand All @@ -215,7 +217,8 @@ export const multipartRequest = <ThrowOnError extends boolean = false>(options?:
...options,
...formDataBodySerializer,
headers: {
'Content-Type': null
'Content-Type': null,
...options?.headers
},
url: '/api/v{api-version}/multipart'
}); };
Expand Down Expand Up @@ -252,7 +255,8 @@ export const putWithFormUrlEncoded = <ThrowOnError extends boolean = false>(opti
...options,
...urlSearchParamsBodySerializer,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
'Content-Type': 'application/x-www-form-urlencoded',
...options?.headers
},
url: '/api/v{api-version}/non-ascii-æøåÆØÅöôêÊ字符串'
}); };
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ export const EnumWithStrings = {
ERROR: 'Error',
_SINGLE_QUOTE_: "'Single Quote'",
_DOUBLE_QUOTES_: '"Double Quotes"',
NON_ASCII__ØÆÅÔÖ_ØÆÅÔÖ字符串: 'Non-ascii: øæåôöØÆÅÔÖ字符串'
'NON_ASCII__ØÆÅÔÖ_ØÆÅÔÖ字符串': 'Non-ascii: øæåôöØÆÅÔÖ字符串'
} as const;

export type EnumWithReplacedCharacters = "'Single Quote'" | '"Double Quotes"' | 'øæåôöØÆÅÔÖ字符串' | 3.1 | '';

export const EnumWithReplacedCharacters = {
_SINGLE_QUOTE_: "'Single Quote'",
_DOUBLE_QUOTES_: '"Double Quotes"',
ØÆÅÔÖ_ØÆÅÔÖ字符串: 'øæåôöØÆÅÔÖ字符串',
'ØÆÅÔÖ_ØÆÅÔÖ字符串': 'øæåôöØÆÅÔÖ字符串',
'_3.1': 3.1,
EMPTY_STRING: ''
} as const;
Expand Down Expand Up @@ -361,7 +361,7 @@ export const foo_bar_enum = {
SUCCESS: 'Success',
WARNING: 'Warning',
ERROR: 'Error',
ØÆÅ字符串: 'ØÆÅ字符串'
'ØÆÅ字符串': 'ØÆÅ字符串'
} as const;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ export const postApiVbyApiVersionFormData = <ThrowOnError extends boolean = fals
...options,
...formDataBodySerializer,
headers: {
'Content-Type': null
'Content-Type': null,
...options?.headers
},
url: '/api/v{api-version}/formData/'
}); };
Expand Down Expand Up @@ -196,7 +197,8 @@ export const uploadFile = <ThrowOnError extends boolean = false>(options: Option
...options,
...urlSearchParamsBodySerializer,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
'Content-Type': 'application/x-www-form-urlencoded',
...options?.headers
},
url: '/api/v{api-version}/upload'
}); };
Expand All @@ -215,7 +217,8 @@ export const multipartRequest = <ThrowOnError extends boolean = false>(options?:
...options,
...formDataBodySerializer,
headers: {
'Content-Type': null
'Content-Type': null,
...options?.headers
},
url: '/api/v{api-version}/multipart'
}); };
Expand Down Expand Up @@ -252,7 +255,8 @@ export const putWithFormUrlEncoded = <ThrowOnError extends boolean = false>(opti
...options,
...urlSearchParamsBodySerializer,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
'Content-Type': 'application/x-www-form-urlencoded',
...options?.headers
},
url: '/api/v{api-version}/non-ascii-æøåÆØÅöôêÊ字符串'
}); };
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ export const EnumWithStrings = {
ERROR: 'Error',
_SINGLE_QUOTE_: "'Single Quote'",
_DOUBLE_QUOTES_: '"Double Quotes"',
NON_ASCII__ØÆÅÔÖ_ØÆÅÔÖ字符串: 'Non-ascii: øæåôöØÆÅÔÖ字符串'
'NON_ASCII__ØÆÅÔÖ_ØÆÅÔÖ字符串': 'Non-ascii: øæåôöØÆÅÔÖ字符串'
} as const;

export type EnumWithReplacedCharacters = "'Single Quote'" | '"Double Quotes"' | 'øæåôöØÆÅÔÖ字符串' | 3.1 | '';

export const EnumWithReplacedCharacters = {
_SINGLE_QUOTE_: "'Single Quote'",
_DOUBLE_QUOTES_: '"Double Quotes"',
ØÆÅÔÖ_ØÆÅÔÖ字符串: 'øæåôöØÆÅÔÖ字符串',
'ØÆÅÔÖ_ØÆÅÔÖ字符串': 'øæåôöØÆÅÔÖ字符串',
'_3.1': 3.1,
EMPTY_STRING: ''
} as const;
Expand Down Expand Up @@ -361,7 +361,7 @@ export const foo_bar_enum = {
SUCCESS: 'Success',
WARNING: 'Warning',
ERROR: 'Error',
ØÆÅ字符串: 'ØÆÅ字符串'
'ØÆÅ字符串': 'ØÆÅ字符串'
} as const;

/**
Expand Down
Loading

0 comments on commit a756987

Please sign in to comment.