-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
97 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,102 @@ | ||
import os from 'os'; | ||
import {Response, Schema, Spec} from 'swagger-schema-official'; | ||
import {inspect} from 'util'; | ||
|
||
class RequestMethod { | ||
public method: string; | ||
public returnType: string; | ||
public url: string; | ||
private readonly spec: Spec; | ||
private readonly responses: Record<string, Response>; | ||
|
||
constructor(url: string, method: string) { | ||
constructor(url: string, method: string, responses: Record<string, Response>, spec: Spec) { | ||
this.url = url; | ||
this.spec = spec; | ||
this.responses = responses; | ||
|
||
if (method === 'delete' || method === 'head') { | ||
this.returnType = 'void'; | ||
} else { | ||
this.returnType = '{}'; | ||
this.returnType = this.buildResponseSchema(); | ||
} | ||
this.method = method; | ||
} | ||
|
||
private buildType(schema: Schema): string { | ||
const emptyObject = '{}'; | ||
|
||
let {required, properties, type} = schema; | ||
|
||
if (schema.$ref && schema.$ref.startsWith('#/definitions')) { | ||
if (!this.spec.definitions) { | ||
console.info('Spec has no definitions.'); | ||
return emptyObject; | ||
} | ||
const definition = schema.$ref.replace('#/definitions', ''); | ||
required = this.spec.definitions[definition].required; | ||
properties = this.spec.definitions[definition].properties; | ||
type = this.spec.definitions[definition].type; | ||
} | ||
|
||
type = type || 'object'; | ||
|
||
switch (type) { | ||
case 'string': | ||
case 'number': { | ||
return type; | ||
} | ||
case 'integer': { | ||
return 'number'; | ||
} | ||
case 'object': { | ||
if (!properties) { | ||
console.info('Schema definition is "object" but has no properties.'); | ||
return emptyObject; | ||
} | ||
|
||
const schema: Record<string, string> = {}; | ||
|
||
for (const property of Object.keys(properties)) { | ||
const propertyName = required && !required.includes(property) ? `${property}?` : property; | ||
schema[propertyName] = this.buildType(properties[property]); | ||
} | ||
|
||
return inspect(schema) | ||
.replace(/'/gm, '') | ||
.replace(',', ';') | ||
.replace(new RegExp(os.EOL, 'g'), ''); | ||
} | ||
case 'array': { | ||
if (!schema.items) { | ||
console.info('Schema definition is "array" but has no items.'); | ||
return 'any[]'; | ||
} | ||
|
||
if (!(schema.items instanceof Array)) { | ||
return this.buildType(schema.items); | ||
} | ||
|
||
const schemes = schema.items.map(itemSchema => this.buildType(itemSchema)).join(','); | ||
return `Array<${schemes}>`; | ||
} | ||
default: { | ||
return emptyObject; | ||
} | ||
} | ||
} | ||
|
||
private buildResponseSchema(): string { | ||
const emptyObject = '{}'; | ||
const response200 = this.responses['200']; | ||
const responseSchema = response200 && response200.schema; | ||
|
||
if (!responseSchema) { | ||
console.info('No schema for code 200 or schema has no definitions.'); | ||
return emptyObject; | ||
} | ||
|
||
return this.buildType(responseSchema); | ||
} | ||
} | ||
|
||
export {RequestMethod}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters