Skip to content

Commit cf9df76

Browse files
committed
Merge branch 'pr'
2 parents 1a8a32b + 67995e9 commit cf9df76

8 files changed

Lines changed: 148 additions & 1 deletion

File tree

aspida.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ module.exports = [
5454
outputEachDir: true,
5555
openapi: { inputFile: 'samples/path.yml' },
5656
},
57+
{
58+
input: 'samples/headers',
59+
outputEachDir: true,
60+
openapi: { inputFile: 'samples/headers.yml' },
61+
},
5762
// {
5863
// input: 'samples/path-at-mark',
5964
// outputEachDir: true,

samples/headers.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
openapi: 3.0.0
2+
info:
3+
version: 1.0.0
4+
title: Sample
5+
paths:
6+
/ping:
7+
get:
8+
responses:
9+
'200':
10+
description: OK
11+
headers:
12+
X-Simple:
13+
$ref: '#/components/headers/X-Simple'
14+
X-Description:
15+
$ref: '#/components/headers/X-Description'
16+
X-Ref:
17+
$ref: '#/components/headers/X-Ref'
18+
content:
19+
application/json:
20+
schema:
21+
type: string
22+
example: pong
23+
components:
24+
headers:
25+
X-Simple:
26+
schema:
27+
type: string
28+
X-Description:
29+
schema:
30+
type: integer
31+
description: This header has a description.
32+
X-Ref:
33+
$ref: '#/components/headers/X-Simple'

samples/headers/$api.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { AspidaClient } from 'aspida';
2+
import type { Methods as Methods0 } from './ping';
3+
4+
const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
5+
const prefix = (baseURL === undefined ? '' : baseURL).replace(/\/$/, '');
6+
const PATH0 = '/ping';
7+
const GET = 'GET';
8+
9+
return {
10+
ping: {
11+
/**
12+
* @returns OK
13+
*/
14+
get: (option?: { config?: T | undefined } | undefined) =>
15+
fetch<Methods0['get']['resBody'], Methods0['get']['resHeaders'], Methods0['get']['status']>(prefix, PATH0, GET, option).text(),
16+
/**
17+
* @returns OK
18+
*/
19+
$get: (option?: { config?: T | undefined } | undefined) =>
20+
fetch<Methods0['get']['resBody'], Methods0['get']['resHeaders'], Methods0['get']['status']>(prefix, PATH0, GET, option).text().then(r => r.body),
21+
$path: () => `${prefix}${PATH0}`,
22+
},
23+
};
24+
};
25+
26+
export type ApiInstance = ReturnType<typeof api>;
27+
export default api;

samples/headers/@types/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* eslint-disable */
2+
export type X_Simple = string
3+
4+
/** This header has a description. */
5+
export type X_Description = number
6+
7+
export type X_Ref = X_Simple

samples/headers/ping/$api.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { AspidaClient } from 'aspida';
2+
import type { Methods as Methods0 } from '.';
3+
4+
const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
5+
const prefix = (baseURL === undefined ? '' : baseURL).replace(/\/$/, '');
6+
const PATH0 = '/ping';
7+
const GET = 'GET';
8+
9+
return {
10+
/**
11+
* @returns OK
12+
*/
13+
get: (option?: { config?: T | undefined } | undefined) =>
14+
fetch<Methods0['get']['resBody'], Methods0['get']['resHeaders'], Methods0['get']['status']>(prefix, PATH0, GET, option).text(),
15+
/**
16+
* @returns OK
17+
*/
18+
$get: (option?: { config?: T | undefined } | undefined) =>
19+
fetch<Methods0['get']['resBody'], Methods0['get']['resHeaders'], Methods0['get']['status']>(prefix, PATH0, GET, option).text().then(r => r.body),
20+
$path: () => `${prefix}${PATH0}`,
21+
};
22+
};
23+
24+
export type ApiInstance = ReturnType<typeof api>;
25+
export default api;

samples/headers/ping/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* eslint-disable */
2+
import type * as Types from '../@types'
3+
4+
export type Methods = {
5+
get: {
6+
status: 200
7+
/** OK */
8+
resBody: string
9+
10+
resHeaders: {
11+
'X-Simple': Types.X_Simple
12+
'X-Description': Types.X_Description
13+
'X-Ref': Types.X_Ref
14+
}
15+
}
16+
}

src/buildV3.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
schema2value,
88
} from './builderUtils/converters';
99
import getDirName from './builderUtils/getDirName';
10+
import headers2Props from './builderUtils/headers2Props';
1011
import parameters2Props from './builderUtils/parameters2Props';
1112
import type { Prop, PropValue } from './builderUtils/props2String';
1213
import { description2Doc, props2String, value2String } from './builderUtils/props2String';
@@ -28,6 +29,7 @@ export default (openapi: OpenAPIV3.Document) => {
2829
const parameters = parameters2Props(openapi.components?.parameters, openapi, false) || [];
2930
const requestBodies = requestBodies2Props(openapi.components?.requestBodies) || [];
3031
const responses = responses2Props(openapi.components?.responses) || [];
32+
const headers = headers2Props(openapi.components?.headers) || [];
3133

3234
files.push(
3335
...Object.keys(openapi.paths)
@@ -371,7 +373,7 @@ export default (openapi: OpenAPIV3.Document) => {
371373
);
372374

373375
const typesText =
374-
parameters.length + schemas.length + requestBodies.length + responses.length
376+
parameters.length + schemas.length + requestBodies.length + responses.length + headers.length
375377
? [
376378
...parameters.map(p => ({
377379
name: p.name,
@@ -399,6 +401,14 @@ export default (openapi: OpenAPIV3.Document) => {
399401
? r.value
400402
: value2String(r.value, '').replace(/\n {2}/g, '\n'),
401403
})),
404+
...headers.map(h => ({
405+
name: h.name,
406+
description: typeof h.value === 'string' ? null : h.value.description,
407+
text:
408+
typeof h.value === 'string'
409+
? h.value
410+
: value2String(h.value, '').replace(/\n {2}/g, '\n'),
411+
})),
402412
]
403413
.map(p => `\n${description2Doc(p.description, '')}export type ${p.name} = ${p.text}\n`)
404414
.join('')

src/builderUtils/headers2Props.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { OpenAPIV3 } from 'openapi-types';
2+
import { $ref2Type, defKey2defName, isRefObject, schema2value } from './converters';
3+
import type { PropValue } from './props2String';
4+
5+
export type Header = { name: string; value: string | PropValue };
6+
7+
export default (headers: OpenAPIV3.ComponentsObject['headers']) =>
8+
headers &&
9+
Object.keys(headers)
10+
.map(defKey => {
11+
const target = headers[defKey];
12+
let value: Header['value'];
13+
14+
if (isRefObject(target)) {
15+
value = $ref2Type(target.$ref);
16+
} else {
17+
const result = schema2value(target.schema, false);
18+
if (!result) return null;
19+
value = { ...result, description: target.description ?? null };
20+
}
21+
22+
return { name: defKey2defName(defKey), value };
23+
})
24+
.filter((v): v is Header => !!v);

0 commit comments

Comments
 (0)