diff --git a/package.json b/package.json index 1d728fee9..e395e6f22 100644 --- a/package.json +++ b/package.json @@ -5,8 +5,8 @@ "author": "Kong ", "homepage": "https://github.com/Kong/httpsnippet", "license": "MIT", - "main": "dist/httpsnippet.js", - "types": "dist/httpsnippet.d.ts", + "main": "dist/index.js", + "types": "dist/index.d.ts", "bin": "bin/httpsnippet", "keywords": [ "api", @@ -51,9 +51,13 @@ "clean": "tsc --build tsconfig.build.json --clean", "prebuild": "npm run clean", "lint": "npm run lint:prettify && npm run lint:code && npm run lint:markdown", - "lint:prettify": "prettier --write .", - "lint:code": "eslint . --ext ts,d.ts,test.ts --fix", + "lint:prettify": "prettier --check .", + "lint:code": "eslint . --ext ts,d.ts,test.ts", "lint:markdown": "markdownlint-cli2 \"**/*.md\" \"#**/node_modules\"", + "lint:fix": "npm run lint:prettify:fix && npm run lint:code:fix && npm run lint:markdown:fix", + "lint:prettify:fix": "prettier --write .", + "lint:code:fix": "eslint . --ext ts,d.ts,test.ts --fix", + "lint:markdown:fix": "markdownlint-cli2-fix \"**/*.md\" \"#**/node_modules\"", "build": "tsc --build tsconfig.build.json", "build:types": "tsc -d --declarationDir dist/lib --declarationMap --emitDeclarationOnly", "test": "jest" diff --git a/src/httpsnippet.ts b/src/httpsnippet.ts index 53171f136..8cb5ccd75 100644 --- a/src/httpsnippet.ts +++ b/src/httpsnippet.ts @@ -53,7 +53,7 @@ interface Entry { request: Partial; } -interface HarEntry { +export interface HarEntry { log: { version: string; creator: { @@ -64,7 +64,7 @@ interface HarEntry { }; } -const isHarEntry = (value: any): value is HarEntry => +export const isHarEntry = (value: any): value is HarEntry => typeof value === 'object' && 'log' in value && typeof value.log === 'object' && diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 000000000..3a865a770 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,28 @@ +export { CodeBuilder, CodeBuilderOptions, PostProcessor } from './helpers/code-builder'; +export { EscapeOptions, escapeString } from './helpers/escape'; +export { HARError, validateHarRequest } from './helpers/har-validator'; +export { getHeader, getHeaderName } from './helpers/headers'; +export { AvailableTarget, availableTargets, extname } from './helpers/utils'; +export { + HarEntry, + HarRequest, + HTTPSnippet, + isHarEntry, + Request, + RequestExtras, +} from './httpsnippet'; +export { + addTarget, + addTargetClient, + Client, + ClientId, + ClientInfo, + Converter, + Extension, + isClient, + isTarget, + Target, + TargetId, + TargetInfo, + targets, +} from './targets/targets'; diff --git a/src/targets/powershell/common.ts b/src/targets/powershell/common.ts index cca01e366..4c3b923a2 100644 --- a/src/targets/powershell/common.ts +++ b/src/targets/powershell/common.ts @@ -26,7 +26,7 @@ export const generatePowershellConvert = (command: PowershellCommand) => { 'PATCH', 'POST', 'PUT', - 'TRACE' + 'TRACE', ]; const methodArg = methods.includes(method.toUpperCase()) ? '-Method' : '-CustomMethod'; @@ -73,7 +73,9 @@ export const generatePowershellConvert = (command: PowershellCommand) => { commandOptions.push(`-Body '${postData.text}'`); } - push(`$response = ${command} -Uri '${fullUrl}' ${methodArg} ${method} ${commandOptions.join(' ')}`); + push( + `$response = ${command} -Uri '${fullUrl}' ${methodArg} ${method} ${commandOptions.join(' ')}`, + ); return join(); }; return convert; diff --git a/src/targets/ruby/faraday/client.ts b/src/targets/ruby/faraday/client.ts index e8e259a4e..493a2d3fc 100644 --- a/src/targets/ruby/faraday/client.ts +++ b/src/targets/ruby/faraday/client.ts @@ -9,7 +9,7 @@ export const faraday: Client = { link: 'https://github.com/lostisland/faraday', description: 'Faraday HTTP client', }, - convert: ({ uriObj, queryObj, method: rawMethod, fullUrl, postData, allHeaders }, options = {}) => { + convert: ({ uriObj, queryObj, method: rawMethod, postData, allHeaders }) => { const { push, blank, join } = new CodeBuilder(); // To support custom methods we check for the supported methods @@ -30,8 +30,8 @@ export const faraday: Client = { 'TRACE', ]; - if(!methods.includes(method)) { - push(`# Faraday cannot currently run ${method} requests. Please use another client.`) + if (!methods.includes(method)) { + push(`# Faraday cannot currently run ${method} requests. Please use another client.`); return join(); } @@ -39,7 +39,7 @@ export const faraday: Client = { blank(); // Write body to beginning of script - if(postData.mimeType === 'application/x-www-form-urlencoded') { + if (postData.mimeType === 'application/x-www-form-urlencoded') { if (postData.params) { push(`data = {`); postData.params.forEach(param => { @@ -52,8 +52,12 @@ export const faraday: Client = { push(`conn = Faraday.new(`); push(` url: '${uriObj.protocol}//${uriObj.host}',`); - if(allHeaders['content-type'] || allHeaders['Content-Type']) { - push(` headers: {'Content-Type' => '${allHeaders['content-type'] || allHeaders['Content-Type']}'}`); + if (allHeaders['content-type'] || allHeaders['Content-Type']) { + push( + ` headers: {'Content-Type' => '${ + allHeaders['content-type'] || allHeaders['Content-Type'] + }'}`, + ); } push(`)`); @@ -63,7 +67,7 @@ export const faraday: Client = { const headers = Object.keys(allHeaders); if (headers.length) { headers.forEach(key => { - if(key.toLowerCase() !== 'content-type') { + if (key.toLowerCase() !== 'content-type') { push(` req.headers['${key}'] = '${escapeForSingleQuotes(allHeaders[key])}'`); } }); @@ -72,9 +76,9 @@ export const faraday: Client = { Object.keys(queryObj).forEach(name => { const value = queryObj[name]; if (Array.isArray(value)) { - push(` req.params['${name}'] = ${JSON.stringify(value)}`) + push(` req.params['${name}'] = ${JSON.stringify(value)}`); } else { - push(` req.params['${name}'] = '${value}'`) + push(` req.params['${name}'] = '${value}'`); } }); @@ -98,7 +102,7 @@ export const faraday: Client = { } push('end'); - blank() + blank(); push('puts response.status'); push('puts response.body'); diff --git a/src/targets/ruby/target.ts b/src/targets/ruby/target.ts index 7550bc650..247c75720 100644 --- a/src/targets/ruby/target.ts +++ b/src/targets/ruby/target.ts @@ -1,6 +1,6 @@ import { Target } from '../targets'; -import { native } from './native/client'; import { faraday } from './faraday/client'; +import { native } from './native/client'; export const ruby: Target = { info: { @@ -11,6 +11,6 @@ export const ruby: Target = { }, clientsById: { native, - faraday + faraday, }, }; diff --git a/tsconfig.build.json b/tsconfig.build.json index 052744692..a25f17e4f 100644 --- a/tsconfig.build.json +++ b/tsconfig.build.json @@ -1,15 +1,16 @@ { "compilerOptions": { - "outDir": "dist", - "rootDir": "src", "allowJs": true, - "resolveJsonModule": true, - "strict": true, - "esModuleInterop": true, + "declaration": true, + "declarationMap": true, "downlevelIteration": true, "lib": ["ESNext"], - "declaration": true, - "declarationMap": true + "esModuleInterop": true, + "outDir": "dist", + "resolveJsonModule": true, + "rootDir": "src", + "sourceMap": true, + "strict": true }, "include": ["src"], "exclude": ["dist", "**/*.test.ts"]