Skip to content
This repository has been archived by the owner on Dec 27, 2024. It is now read-only.

Commit

Permalink
feat: add ts definitions to library
Browse files Browse the repository at this point in the history
  • Loading branch information
joolfe committed May 18, 2021
1 parent 704ef36 commit 36cf16b
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run lint
- run: npm run lint:ts
- run: npm test
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
Expand Down
22 changes: 21 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
"version": "1.8.0",
"description": "Convert postman collection to OpenAPI spec",
"main": "lib/index.js",
"types": "types/index.d.ts",
"bin": {
"p2o": "./bin/cli.js"
},
"scripts": {
"lint": "eslint **/*.js",
"lint:fix": "eslint **/*.js --fix",
"lint:ts": "tsc --build types",
"test:unit": "mocha",
"test:unit-no-only": "npm run test:unit -- --forbid-only",
"test": "nyc npm run test:unit-no-only",
Expand Down Expand Up @@ -53,7 +55,8 @@
"execa": "^5.0.0",
"husky": "^6.0.0",
"mocha": "^8.3.2",
"nyc": "^15.1.0"
"nyc": "^15.1.0",
"typescript": "^4.2.4"
},
"commitlint": {
"extends": [
Expand Down
83 changes: 83 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@

/**
* Object that contain the license information as defined in
* https://spec.openapis.org/oas/v3.0.3.html#contactObject
*/
export interface ContactObject {
name?: string,
url?: string,
email?: string
}

/**
* Object that contain the license information as defined in
* https://spec.openapis.org/oas/v3.0.3.html#licenseObject
*/
export interface LicenseObject {
name: string,
url?: string
}

/**
* Object that contain the api information as defined in
* https://spec.openapis.org/oas/v3.0.3.html#info-object
*/
export interface InfoObject {
title?: string,
description?: string,
termsOfService?: string,
contact?: ContactObject,
license?: LicenseObject,
version?: string,
}

/**
* Object that contain the servers information as defined in
* https://spec.openapis.org/oas/v3.0.3.html#server-object
* field `variables`is not supported yet
*/
export interface ServerObject {
url: string,
description?: string
}

/**
* Object that contain the servers information as defined in
* https://spec.openapis.org/oas/v3.0.3.html#security-scheme-object
*/
export interface SecurityObject {
type: 'http',
description?: string,
scheme: 'bearer' | 'basic',
bearerFormat?: string
}

export interface AuthOptions {
[key: string]: SecurityObject
}

/**
* Object that contain the external docs information as defined in
* https://spec.openapis.org/oas/v3.0.3.html#external-documentation-object
*/
export interface ExternalDocsObject {
description?: string,
url?: string
}

export interface FoldersOption {
concat: boolean,
separator: string
}

export interface Options {
info?: InfoObject,
defaultTag?: string,
pathDepth?: number,
auth?: AuthOptions,
servers?: Array<ServerObject>,
externalDocs?: ExternalDocsObject,
folders?: FoldersOption
}

export function postmanToOpenApi (input: string, output?: string, options?: Options) : string
20 changes: 20 additions & 0 deletions types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["ES6"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": ["../"],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true

},
"files": [
"index.d.ts",
"typescript-test.ts"
]
}
85 changes: 85 additions & 0 deletions types/typescript-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
'use strict'

import {
Options, ContactObject, LicenseObject, InfoObject,
ServerObject, SecurityObject, ExternalDocsObject,
FoldersOption, postmanToOpenApi
} from './index.d'

const contact:ContactObject = {
name: 'My Enterprise',
url: 'http://fancyEnterprise.com',
email: 'support@myfancy.com'
}

const license:LicenseObject = {
name: 'MIT',
url: 'http://fancyEnterprise.com/mit'
}

const info:InfoObject = {
title: 'Amazing API',
description: 'The best API you can find out there',
termsOfService: 'http://fancyEnterprise.com/tos',
contact,
license,
version: '1.0.1'
}

const serverDev: ServerObject = {
url: 'http://fancyEnterprise.com/dev',
description: 'Development environment'
}

const serverPro: ServerObject = {
url: 'http://fancyEnterprise.com/api',
description: 'Live environment'
}

const basicAuth:SecurityObject = {
type: 'http',
description: 'Basic authentication using user and password',
scheme: 'basic'
}

const bearerAuth:SecurityObject = {
type: 'http',
description: 'Bearer authentication',
scheme: 'bearer',
bearerFormat: 'Signed JWT'
}

const externalDocs:ExternalDocsObject = {
description: 'Fancy API documentation in detail',
url: 'http://fancyEnterprise.com/dev'
}

const folders:FoldersOption = {
concat: true,
separator: '->'
}

const options:Options = {
info,
defaultTag: 'my Tag',
pathDepth: 0,
auth: {
'basicAuth': basicAuth,
'bearerAuth': bearerAuth
},
servers: [serverDev, serverPro],
externalDocs,
folders
};

(async () => {

const openAPI1 = postmanToOpenApi('./path/to/postman_collection.json')

const openAPI2 = postmanToOpenApi('./path/to/postman_collection.json', './path/to/result/openApi.yml')

const openAPI3 = postmanToOpenApi('./path/to/postman_collection.json', './path/to/result/openApi.yml', options)

})();
// TODO what is really mandatory or not as is not the same that the object is mandatory
// and then inside the object what should be complete, example type in auth?

0 comments on commit 36cf16b

Please sign in to comment.