Skip to content

Commit

Permalink
Merge branch 'pr'
Browse files Browse the repository at this point in the history
  • Loading branch information
solufa committed Aug 4, 2023
2 parents 1a8a32b + 67995e9 commit cf9df76
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 1 deletion.
5 changes: 5 additions & 0 deletions aspida.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ module.exports = [
outputEachDir: true,
openapi: { inputFile: 'samples/path.yml' },
},
{
input: 'samples/headers',
outputEachDir: true,
openapi: { inputFile: 'samples/headers.yml' },
},
// {
// input: 'samples/path-at-mark',
// outputEachDir: true,
Expand Down
33 changes: 33 additions & 0 deletions samples/headers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
openapi: 3.0.0
info:
version: 1.0.0
title: Sample
paths:
/ping:
get:
responses:
'200':
description: OK
headers:
X-Simple:
$ref: '#/components/headers/X-Simple'
X-Description:
$ref: '#/components/headers/X-Description'
X-Ref:
$ref: '#/components/headers/X-Ref'
content:
application/json:
schema:
type: string
example: pong
components:
headers:
X-Simple:
schema:
type: string
X-Description:
schema:
type: integer
description: This header has a description.
X-Ref:
$ref: '#/components/headers/X-Simple'
27 changes: 27 additions & 0 deletions samples/headers/$api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { AspidaClient } from 'aspida';
import type { Methods as Methods0 } from './ping';

const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
const prefix = (baseURL === undefined ? '' : baseURL).replace(/\/$/, '');
const PATH0 = '/ping';
const GET = 'GET';

return {
ping: {
/**
* @returns OK
*/
get: (option?: { config?: T | undefined } | undefined) =>
fetch<Methods0['get']['resBody'], Methods0['get']['resHeaders'], Methods0['get']['status']>(prefix, PATH0, GET, option).text(),
/**
* @returns OK
*/
$get: (option?: { config?: T | undefined } | undefined) =>
fetch<Methods0['get']['resBody'], Methods0['get']['resHeaders'], Methods0['get']['status']>(prefix, PATH0, GET, option).text().then(r => r.body),
$path: () => `${prefix}${PATH0}`,
},
};
};

export type ApiInstance = ReturnType<typeof api>;
export default api;
7 changes: 7 additions & 0 deletions samples/headers/@types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* eslint-disable */
export type X_Simple = string

/** This header has a description. */
export type X_Description = number

export type X_Ref = X_Simple
25 changes: 25 additions & 0 deletions samples/headers/ping/$api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { AspidaClient } from 'aspida';
import type { Methods as Methods0 } from '.';

const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
const prefix = (baseURL === undefined ? '' : baseURL).replace(/\/$/, '');
const PATH0 = '/ping';
const GET = 'GET';

return {
/**
* @returns OK
*/
get: (option?: { config?: T | undefined } | undefined) =>
fetch<Methods0['get']['resBody'], Methods0['get']['resHeaders'], Methods0['get']['status']>(prefix, PATH0, GET, option).text(),
/**
* @returns OK
*/
$get: (option?: { config?: T | undefined } | undefined) =>
fetch<Methods0['get']['resBody'], Methods0['get']['resHeaders'], Methods0['get']['status']>(prefix, PATH0, GET, option).text().then(r => r.body),
$path: () => `${prefix}${PATH0}`,
};
};

export type ApiInstance = ReturnType<typeof api>;
export default api;
16 changes: 16 additions & 0 deletions samples/headers/ping/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* eslint-disable */
import type * as Types from '../@types'

export type Methods = {
get: {
status: 200
/** OK */
resBody: string

resHeaders: {
'X-Simple': Types.X_Simple
'X-Description': Types.X_Description
'X-Ref': Types.X_Ref
}
}
}
12 changes: 11 additions & 1 deletion src/buildV3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
schema2value,
} from './builderUtils/converters';
import getDirName from './builderUtils/getDirName';
import headers2Props from './builderUtils/headers2Props';
import parameters2Props from './builderUtils/parameters2Props';
import type { Prop, PropValue } from './builderUtils/props2String';
import { description2Doc, props2String, value2String } from './builderUtils/props2String';
Expand All @@ -28,6 +29,7 @@ export default (openapi: OpenAPIV3.Document) => {
const parameters = parameters2Props(openapi.components?.parameters, openapi, false) || [];
const requestBodies = requestBodies2Props(openapi.components?.requestBodies) || [];
const responses = responses2Props(openapi.components?.responses) || [];
const headers = headers2Props(openapi.components?.headers) || [];

files.push(
...Object.keys(openapi.paths)
Expand Down Expand Up @@ -371,7 +373,7 @@ export default (openapi: OpenAPIV3.Document) => {
);

const typesText =
parameters.length + schemas.length + requestBodies.length + responses.length
parameters.length + schemas.length + requestBodies.length + responses.length + headers.length
? [
...parameters.map(p => ({
name: p.name,
Expand Down Expand Up @@ -399,6 +401,14 @@ export default (openapi: OpenAPIV3.Document) => {
? r.value
: value2String(r.value, '').replace(/\n {2}/g, '\n'),
})),
...headers.map(h => ({
name: h.name,
description: typeof h.value === 'string' ? null : h.value.description,
text:
typeof h.value === 'string'
? h.value
: value2String(h.value, '').replace(/\n {2}/g, '\n'),
})),
]
.map(p => `\n${description2Doc(p.description, '')}export type ${p.name} = ${p.text}\n`)
.join('')
Expand Down
24 changes: 24 additions & 0 deletions src/builderUtils/headers2Props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { OpenAPIV3 } from 'openapi-types';
import { $ref2Type, defKey2defName, isRefObject, schema2value } from './converters';
import type { PropValue } from './props2String';

export type Header = { name: string; value: string | PropValue };

export default (headers: OpenAPIV3.ComponentsObject['headers']) =>
headers &&
Object.keys(headers)
.map(defKey => {
const target = headers[defKey];
let value: Header['value'];

if (isRefObject(target)) {
value = $ref2Type(target.$ref);
} else {
const result = schema2value(target.schema, false);
if (!result) return null;
value = { ...result, description: target.description ?? null };
}

return { name: defKey2defName(defKey), value };
})
.filter((v): v is Header => !!v);

0 comments on commit cf9df76

Please sign in to comment.