Skip to content

Commit 96ecab7

Browse files
committed
ci: fix build
1 parent 4b2d5e1 commit 96ecab7

File tree

3 files changed

+122
-26
lines changed

3 files changed

+122
-26
lines changed

src/index.ts

+21-11
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,30 @@ const log = console.log.bind(console, '[SWAGGER-CODEGEN-TS]:');
1313

1414
export type TGenerateOptions = {
1515
/**
16-
* Absolute path to json spec
16+
* Path to json spec
1717
*/
1818
pathToSpec: string;
1919
/**
20-
* Absolute path to output directory (should be empty)
20+
* Path to output directory (should be empty)
2121
*/
2222
out: string;
2323
/**
2424
* Spec serializer
2525
*/
2626
serialize: TSerializer;
27+
/**
28+
* Path to prettier config
29+
*/
30+
pathToPrettierConfig?: string;
2731
};
2832

33+
const cwd = process.cwd();
34+
const resolvePath = (p: string) => (path.isAbsolute(p) ? p : path.resolve(cwd, p));
35+
2936
export async function generate(options: TGenerateOptions): Promise<void> {
30-
const { pathToSpec, out, serialize } = options;
37+
const { serialize, pathToPrettierConfig } = options;
38+
const out = resolvePath(options.out);
39+
const pathToSpec = resolvePath(options.pathToSpec);
3140
log('Reading spec from', pathToSpec);
3241
const buffer = await fs.readFile(pathToSpec);
3342
log('Parsing spec');
@@ -42,18 +51,19 @@ export async function generate(options: TGenerateOptions): Promise<void> {
4251
return;
4352
}
4453
log('Serializing spec');
45-
const specName = path.dirname(out);
46-
const serialized = serialize(specName, decoded.value);
54+
const serialized = serialize(path.basename(out), decoded.value);
4755
log('Running prettier');
48-
const prettierConfig = fromNullable(await prettier.resolveConfig(path.resolve(__dirname, '../.prettierrc')));
56+
const prettierConfig = fromNullable(
57+
await prettier.resolveConfig(
58+
fromNullable(pathToPrettierConfig)
59+
.map(resolvePath)
60+
.getOrElseL(() => path.resolve(__dirname, '../.prettierrc')),
61+
),
62+
);
4963
const formatted = prettierConfig
5064
.map(config => map(serialized, content => prettier.format(content, config)))
5165
.getOrElse(serialized);
5266
log('Writing to', out);
53-
const destination = path.basename(out);
54-
if (!fs.pathExists(destination)) {
55-
await fs.mkdirp(destination);
56-
await write(destination, formatted);
57-
}
67+
await write(path.dirname(out), formatted);
5868
log('Done.');
5969
}

src/swagger.ts

+99-14
Original file line numberDiff line numberDiff line change
@@ -606,29 +606,114 @@ export const ResponsesDefinitionsObject: t.Type<TResponsesDefinitionsObject, mix
606606
ResponseObject,
607607
);
608608

609-
export type TScopeaObject = TDictionary<string>;
610-
export const ScopesObject: t.Type<TScopeaObject, mixed> = t.dictionary(t.string, t.string);
609+
export type TScopesObject = TDictionary<string>;
610+
export const ScopesObject: t.Type<TScopesObject, mixed> = t.dictionary(t.string, t.string);
611611

612-
export type TSecuritySchemeObject = {
613-
type: 'basic' | 'apiKey' | 'oauth2';
612+
//#region SecuritySchemeObject
613+
614+
export type TBaseSecuritySchemeObjectProps = {
614615
description: Option<string>;
615-
name: string;
616-
in: 'query' | 'header';
617-
flow: 'implicit' | 'password' | 'application' | 'accessCode';
618-
authorizationUrl: string;
619-
tokenUrl: string;
620-
scopes: TScopeaObject;
621616
};
622-
export const SecuritySchemeObject: t.Type<TSecuritySchemeObject, mixed> = t.type({
623-
type: t.union([t.literal('basic'), t.literal('apiKey'), t.literal('oauth2')]),
617+
const BaseSecuritySchemeObjectProps = {
624618
description: stringOption,
625-
name: t.string,
619+
};
620+
(): t.Type<TBaseSecuritySchemeObjectProps, mixed> => t.type(BaseSecuritySchemeObjectProps); //tslint:disable-line no-unused-expression (integrity check)
621+
622+
export type TBasicSecuritySchemeObject = TBaseSecuritySchemeObjectProps & {
623+
type: 'basic';
624+
};
625+
const BasicSecuritySchemeObject: t.Tagged<'type', TBasicSecuritySchemeObject, mixed> = t.type({
626+
...BaseSecuritySchemeObjectProps,
627+
type: t.literal('basic'),
628+
});
629+
630+
export type TApiKeySecuritySchemeObject = TBaseSecuritySchemeObjectProps & {
631+
type: 'apiKey';
632+
in: 'query' | 'header';
633+
name: string;
634+
};
635+
const ApiKeySecuritySchemeObject: t.Tagged<'type', TApiKeySecuritySchemeObject, mixed> = t.type({
636+
...BaseSecuritySchemeObjectProps,
637+
type: t.literal('apiKey'),
626638
in: t.union([t.literal('query'), t.literal('header')]),
627-
flow: t.union([t.literal('implicit'), t.literal('password'), t.literal('application'), t.literal('accessCode')]),
639+
name: t.string,
640+
});
641+
642+
export type TImplicitOAuth2SecuritySchemeObject = TBaseSecuritySchemeObjectProps & {
643+
type: 'oauth2';
644+
flow: 'implicit';
645+
authorizationUrl: string;
646+
scopes: TScopesObject;
647+
};
648+
const ImplicitOAuth2SecuritySchemeObject: t.Tagged<'flow', TImplicitOAuth2SecuritySchemeObject, mixed> = t.type({
649+
...BaseSecuritySchemeObjectProps,
650+
type: t.literal('oauth2'),
651+
flow: t.literal('implicit'),
628652
authorizationUrl: t.string,
653+
scopes: ScopesObject,
654+
});
655+
export type TPasswordOAuth2SecuritySchemeObject = TBaseSecuritySchemeObjectProps & {
656+
type: 'oauth2';
657+
flow: 'password';
658+
tokenUrl: string;
659+
scopes: TScopesObject;
660+
};
661+
const PasswordOAuth2SecuritySchemeObject: t.Tagged<'flow', TPasswordOAuth2SecuritySchemeObject, mixed> = t.type({
662+
...BaseSecuritySchemeObjectProps,
663+
type: t.literal('oauth2'),
664+
flow: t.literal('password'),
665+
tokenUrl: t.string,
666+
scopes: ScopesObject,
667+
});
668+
export type TApplicationOAuth2SecuritySchemeObject = TBaseSecuritySchemeObjectProps & {
669+
type: 'oauth2';
670+
flow: 'application';
671+
tokenUrl: string;
672+
scopes: TScopesObject;
673+
};
674+
const ApplicationOAuth2SecuritySchemeObject: t.Tagged<'flow', TApplicationOAuth2SecuritySchemeObject, mixed> = t.type({
675+
...BaseSecuritySchemeObjectProps,
676+
type: t.literal('oauth2'),
677+
flow: t.literal('application'),
629678
tokenUrl: t.string,
630679
scopes: ScopesObject,
631680
});
681+
export type TAccessCodeOAuth2SecuritySchemeObject = TBaseSecuritySchemeObjectProps & {
682+
type: 'oauth2';
683+
flow: 'accessCode';
684+
tokenUrl: string;
685+
scopes: TScopesObject;
686+
};
687+
const AccessCodeOAuth2SecuritySchemeObject: t.Tagged<'flow', TAccessCodeOAuth2SecuritySchemeObject, mixed> = t.type({
688+
...BaseSecuritySchemeObjectProps,
689+
type: t.literal('oauth2'),
690+
flow: t.literal('accessCode'),
691+
tokenUrl: t.string,
692+
scopes: ScopesObject,
693+
});
694+
export type TOAuth2SecuritySchemeObject =
695+
| TImplicitOAuth2SecuritySchemeObject
696+
| TPasswordOAuth2SecuritySchemeObject
697+
| TApplicationOAuth2SecuritySchemeObject
698+
| TAccessCodeOAuth2SecuritySchemeObject;
699+
const OAuth2SecuritySchemeObject: t.Tagged<'type', TOAuth2SecuritySchemeObject, mixed> = t.taggedUnion('flow', [
700+
ImplicitOAuth2SecuritySchemeObject,
701+
PasswordOAuth2SecuritySchemeObject,
702+
ApplicationOAuth2SecuritySchemeObject,
703+
AccessCodeOAuth2SecuritySchemeObject,
704+
]) as any;
705+
706+
export type TSecuritySchemeObject =
707+
| TBasicSecuritySchemeObject
708+
| TApiKeySecuritySchemeObject
709+
| TOAuth2SecuritySchemeObject;
710+
const SecuritySchemeObject: t.Type<TSecuritySchemeObject, mixed> = t.taggedUnion('type', [
711+
BasicSecuritySchemeObject,
712+
ApiKeySecuritySchemeObject,
713+
OAuth2SecuritySchemeObject,
714+
]);
715+
716+
//#endregion
632717

633718
export type TSecurityDefinitionsObject = TDictionary<TSecuritySchemeObject>;
634719
export const SecurityDefinitionsObject: t.Type<TSecurityDefinitionsObject, mixed> = t.dictionary(

test/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ const self = path.resolve(__dirname);
88

99
generate({
1010
pathToSpec: path.resolve(self, './swagger.json'),
11-
out: path.resolve(self, './out/spec'),
11+
out: path.resolve(self, './spec'),
1212
serialize,
1313
}).catch(error => {
14+
console.error(error);
1415
process.exit(1);
1516
});

0 commit comments

Comments
 (0)