Skip to content

Commit 12bc295

Browse files
authored
Merge pull request #1896 from hey-api/feat/typescript-read-write-only
feat: support read-only and write-only properties
2 parents 135f3ac + 8840ed7 commit 12bc295

File tree

181 files changed

+9583
-8461
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

181 files changed

+9583
-8461
lines changed

.changeset/lemon-spies-hammer.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
'@hey-api/openapi-ts': minor
3+
---
4+
5+
feat: support read-only and write-only properties
6+
7+
### Read-only and write-only fields
8+
9+
Starting with v0.66.0, `@hey-api/typescript` will generate separate types for payloads and responses if it detects any read-only or write-only fields. To preserve the previous behavior and generate a single type regardless, set `readOnlyWriteOnlyBehavior` to `off`.
10+
11+
```js
12+
export default {
13+
input: 'https://get.heyapi.dev/hey-api/backend',
14+
output: 'src/client',
15+
plugins: [
16+
// ...other plugins
17+
{
18+
name: '@hey-api/typescript',
19+
readOnlyWriteOnlyBehavior: 'off', // [!code ++]
20+
},
21+
],
22+
};
23+
```

docs/openapi-ts/migrating.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,26 @@ This config option is deprecated and will be removed in favor of [clients](./cli
2727

2828
This config option is deprecated and will be removed.
2929

30+
## v0.66.0
31+
32+
### Read-only and write-only fields
33+
34+
Starting with v0.66.0, `@hey-api/typescript` will generate separate types for payloads and responses if it detects any read-only or write-only fields. To preserve the previous behavior and generate a single type regardless, set `readOnlyWriteOnlyBehavior` to `off`.
35+
36+
```js
37+
export default {
38+
input: 'https://get.heyapi.dev/hey-api/backend',
39+
output: 'src/client',
40+
plugins: [
41+
// ...other plugins
42+
{
43+
name: '@hey-api/typescript',
44+
readOnlyWriteOnlyBehavior: 'off', // [!code ++]
45+
},
46+
],
47+
};
48+
```
49+
3050
## v0.64.0
3151

3252
### Added `ClientOptions` interface

packages/openapi-ts-tests/test/2.0.x.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,14 @@ describe(`OpenAPI ${version}`, () => {
211211
}),
212212
description: 'handles form data',
213213
},
214+
{
215+
config: createConfig({
216+
input: 'read-write-only.yaml',
217+
output: 'read-write-only',
218+
plugins: ['@hey-api/client-fetch', '@hey-api/typescript'],
219+
}),
220+
description: 'handles read-only and write-only types',
221+
},
214222
{
215223
config: createConfig({
216224
input: 'schema-unknown.yaml',

packages/openapi-ts-tests/test/3.0.x.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,14 @@ describe(`OpenAPI ${version}`, () => {
420420
}),
421421
description: 'handles non-exploded array query parameters (Axios)',
422422
},
423+
{
424+
config: createConfig({
425+
input: 'read-write-only.yaml',
426+
output: 'read-write-only',
427+
plugins: ['@hey-api/client-fetch', '@hey-api/typescript'],
428+
}),
429+
description: 'handles read-only and write-only types',
430+
},
423431
{
424432
config: createConfig({
425433
input: 'security-api-key.json',

packages/openapi-ts-tests/test/3.1.x.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,14 @@ describe(`OpenAPI ${version}`, () => {
466466
}),
467467
description: 'handles non-exploded array query parameters (Axios)',
468468
},
469+
{
470+
config: createConfig({
471+
input: 'read-write-only.yaml',
472+
output: 'read-write-only',
473+
plugins: ['@hey-api/client-fetch', '@hey-api/typescript'],
474+
}),
475+
description: 'handles read-only and write-only types',
476+
},
469477
{
470478
config: createConfig({
471479
input: 'required-all-of-ref.json',

packages/openapi-ts-tests/test/__snapshots__/2.0.x/plugins/@hey-api/sdk/default/types.gen.ts

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,15 @@ export type ModelWithNestedEnums = {
267267
/**
268268
* This is a model with one property containing a reference
269269
*/
270-
export type ModelWithReference = {
271-
prop?: ModelWithProperties;
270+
export type ModelWithReferenceReadable = {
271+
prop?: ModelWithPropertiesReadable;
272+
};
273+
274+
/**
275+
* This is a model with one property containing a reference
276+
*/
277+
export type ModelWithReferenceWritable = {
278+
prop?: ModelWithPropertiesWritable;
272279
};
273280

274281
/**
@@ -299,7 +306,7 @@ export type ModelWithCircularReference = {
299306
/**
300307
* This is a model with one nested property
301308
*/
302-
export type ModelWithProperties = {
309+
export type ModelWithPropertiesReadable = {
303310
required: string;
304311
readonly requiredAndReadOnly: string;
305312
string?: string;
@@ -316,7 +323,21 @@ export type ModelWithProperties = {
316323
/**
317324
* This is a model with one nested property
318325
*/
319-
export type ModelWithNestedProperties = {
326+
export type ModelWithPropertiesWritable = {
327+
required: string;
328+
string?: string;
329+
number?: number;
330+
boolean?: boolean;
331+
reference?: ModelWithString;
332+
'property with space'?: string;
333+
default?: string;
334+
try?: string;
335+
};
336+
337+
/**
338+
* This is a model with one nested property
339+
*/
340+
export type ModelWithNestedPropertiesReadable = {
320341
readonly first: {
321342
readonly second: {
322343
readonly third: string;
@@ -372,7 +393,7 @@ export type Default = {
372393
/**
373394
* This is a model that contains a some patterns
374395
*/
375-
export type ModelWithPattern = {
396+
export type ModelWithPatternReadable = {
376397
key: string;
377398
name: string;
378399
readonly enabled?: boolean;
@@ -384,6 +405,19 @@ export type ModelWithPattern = {
384405
patternWithBacktick?: string;
385406
};
386407

408+
/**
409+
* This is a model that contains a some patterns
410+
*/
411+
export type ModelWithPatternWritable = {
412+
key: string;
413+
name: string;
414+
id?: string;
415+
text?: string;
416+
patternWithSingleQuotes?: string;
417+
patternWithNewline?: string;
418+
patternWithBacktick?: string;
419+
};
420+
387421
export type ParameterActivityParams = {
388422
description?: string;
389423
graduate_id?: number;

packages/openapi-ts-tests/test/__snapshots__/2.0.x/plugins/@hey-api/sdk/throwOnError/types.gen.ts

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,15 @@ export type ModelWithNestedEnums = {
267267
/**
268268
* This is a model with one property containing a reference
269269
*/
270-
export type ModelWithReference = {
271-
prop?: ModelWithProperties;
270+
export type ModelWithReferenceReadable = {
271+
prop?: ModelWithPropertiesReadable;
272+
};
273+
274+
/**
275+
* This is a model with one property containing a reference
276+
*/
277+
export type ModelWithReferenceWritable = {
278+
prop?: ModelWithPropertiesWritable;
272279
};
273280

274281
/**
@@ -299,7 +306,7 @@ export type ModelWithCircularReference = {
299306
/**
300307
* This is a model with one nested property
301308
*/
302-
export type ModelWithProperties = {
309+
export type ModelWithPropertiesReadable = {
303310
required: string;
304311
readonly requiredAndReadOnly: string;
305312
string?: string;
@@ -316,7 +323,21 @@ export type ModelWithProperties = {
316323
/**
317324
* This is a model with one nested property
318325
*/
319-
export type ModelWithNestedProperties = {
326+
export type ModelWithPropertiesWritable = {
327+
required: string;
328+
string?: string;
329+
number?: number;
330+
boolean?: boolean;
331+
reference?: ModelWithString;
332+
'property with space'?: string;
333+
default?: string;
334+
try?: string;
335+
};
336+
337+
/**
338+
* This is a model with one nested property
339+
*/
340+
export type ModelWithNestedPropertiesReadable = {
320341
readonly first: {
321342
readonly second: {
322343
readonly third: string;
@@ -372,7 +393,7 @@ export type Default = {
372393
/**
373394
* This is a model that contains a some patterns
374395
*/
375-
export type ModelWithPattern = {
396+
export type ModelWithPatternReadable = {
376397
key: string;
377398
name: string;
378399
readonly enabled?: boolean;
@@ -384,6 +405,19 @@ export type ModelWithPattern = {
384405
patternWithBacktick?: string;
385406
};
386407

408+
/**
409+
* This is a model that contains a some patterns
410+
*/
411+
export type ModelWithPatternWritable = {
412+
key: string;
413+
name: string;
414+
id?: string;
415+
text?: string;
416+
patternWithSingleQuotes?: string;
417+
patternWithNewline?: string;
418+
patternWithBacktick?: string;
419+
};
420+
387421
export type ParameterActivityParams = {
388422
description?: string;
389423
graduate_id?: number;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
import type { ClientOptions } from './types.gen';
4+
import { type Config, type ClientOptions as DefaultClientOptions, createClient, createConfig } from '@hey-api/client-fetch';
5+
6+
/**
7+
* The `createClientConfig()` function will be called on client initialization
8+
* and the returned object will become the client's initial configuration.
9+
*
10+
* You may want to initialize your client this way instead of calling
11+
* `setConfig()`. This is useful for example if you're using Next.js
12+
* to ensure your client always has the correct values.
13+
*/
14+
export type CreateClientConfig<T extends DefaultClientOptions = ClientOptions> = (override?: Config<DefaultClientOptions & T>) => Config<Required<DefaultClientOptions> & T>;
15+
16+
export const client = createClient(createConfig<ClientOptions>());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
export * from './types.gen';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
export type ReadableFooRead = ReadableBarRead & {
4+
readonly foo?: string;
5+
};
6+
7+
export type WritableFooRead = WritableBarRead;
8+
9+
export type ReadableBarRead = Baz & {
10+
readonly bar?: string;
11+
};
12+
13+
export type WritableBarRead = Baz;
14+
15+
export type Baz = {
16+
baz?: string;
17+
};
18+
19+
export type PostFooReadData = {
20+
body: WritableFooRead;
21+
path?: never;
22+
query?: never;
23+
url: '/foo-read';
24+
};
25+
26+
export type PostFooReadResponses = {
27+
/**
28+
* OK
29+
*/
30+
200: ReadableFooRead;
31+
};
32+
33+
export type PostFooReadResponse = PostFooReadResponses[keyof PostFooReadResponses];
34+
35+
export type ClientOptions = {
36+
baseUrl: string;
37+
};

0 commit comments

Comments
 (0)