UNPKG

insomnia-importers

Version:

Various data importers for Insomnia

563 lines 26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.convert = exports.ImportPostman = exports.description = exports.name = exports.id = void 0; const ramda_1 = require("ramda"); exports.id = 'postman'; exports.name = 'Postman'; exports.description = 'Importer for Postman collections'; let requestCount = 1; let requestGroupCount = 1; const POSTMAN_SCHEMA_V2_0 = 'https://schema.getpostman.com/json/collection/v2.0.0/collection.json'; const POSTMAN_SCHEMA_V2_1 = 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json'; class ImportPostman { constructor(collection) { this.importVariable = (variables) => { if ((variables === null || variables === void 0 ? void 0 : variables.length) === 0) { return null; } const variable = {}; for (let i = 0; i < variables.length; i++) { const key = variables[i].key; if (key === undefined) { continue; } variable[key] = variables[i].value; } return variable; }; this.importItems = (items, parentId = '__WORKSPACE_ID__') => { // @ts-expect-error this is because there are devergent behaviors for how the function treats this collection. This is handled appropriately in the function itself in different branches. return items.reduce((accumulator, item) => { if (Object.prototype.hasOwnProperty.call(item, 'request')) { return [...accumulator, this.importRequestItem(item, parentId)]; } const requestGroup = this.importFolderItem(item, parentId); return [ ...accumulator, requestGroup, ...this.importItems(item.item, requestGroup._id), ]; }, []); }; this.importRequestItem = ({ request, name = '' }, parentId) => { var _a; if (typeof request === 'string') { return {}; } const { authentication, headers } = this.importAuthentication(request.auth, request.header); return { parentId, _id: `__REQ_${requestCount++}__`, _type: 'request', name, description: request.description || '', url: this.importUrl(request.url), method: request.method || 'GET', headers: headers.map(({ key, value }) => ({ name: key, value, })), body: this.importBody(request.body, (_a = headers.find(({ key }) => key === 'Content-Type')) === null || _a === void 0 ? void 0 : _a.value), authentication, }; }; this.importFolderItem = ({ name, description }, parentId) => { return { parentId, _id: `__GRP_${requestGroupCount++}__`, _type: 'request_group', name, description: description || '', }; }; this.importCollection = () => { const { item, info: { name, description }, variable, } = this.collection; const postmanVariable = this.importVariable(variable || []); const collectionFolder = { parentId: '__WORKSPACE_ID__', _id: `__GRP_${requestGroupCount++}__`, _type: 'request_group', name, description: typeof description === 'string' ? description : '', }; if (postmanVariable) { collectionFolder.variable = postmanVariable; } return [collectionFolder, ...this.importItems(item, collectionFolder._id)]; }; this.importUrl = (url) => { if (!url) { return ''; } if (typeof url === 'object' && url.raw) { return url.raw; } if (typeof url === 'string') { return url; } return ''; }; this.importBody = (body, contentType) => { if (!body) { return {}; } switch (body.mode) { case 'raw': return this.importBodyRaw(body.raw, contentType); case 'urlencoded': return this.importBodyFormUrlEncoded(body.urlencoded); case 'formdata': // TODO: Handle this as properly as multipart/form-data return this.importBodyFormdata(body.formdata); case 'graphql': return this.importBodyGraphQL(body.graphql); default: return {}; } }; this.importBodyFormdata = (formdata) => { const { schema } = this.collection.info; const params = formdata === null || formdata === void 0 ? void 0 : formdata.map(({ key, value, type, enabled, disabled, src }) => { const item = { type, name: key, }; if (schema === POSTMAN_SCHEMA_V2_0) { item.disabled = !enabled; } else if (schema === POSTMAN_SCHEMA_V2_1) { item.disabled = !!disabled; } if (type === 'file') { item.fileName = src; } else { item.value = value; } return item; }); return { params, mimeType: 'multipart/form-data', }; }; this.importBodyFormUrlEncoded = (urlEncoded) => { const { schema } = this.collection.info; const params = urlEncoded === null || urlEncoded === void 0 ? void 0 : urlEncoded.map(({ key, value, enabled, disabled }) => { const item = { value, name: key, }; if (schema === POSTMAN_SCHEMA_V2_0) { item.disabled = !enabled; } else if (schema === POSTMAN_SCHEMA_V2_1) { item.disabled = !!disabled; } return item; }); return { params, mimeType: 'application/x-www-form-urlencoded', }; }; this.importBodyRaw = (raw, mimeType = '') => { if (raw === '') { return {}; } return { mimeType, text: raw, }; }; this.importBodyGraphQL = (graphql) => { if (!graphql) { return {}; } return { mimeType: 'application/graphql', text: JSON.stringify(graphql), }; }; this.importAuthentication = (authentication, originalHeaders = []) => { var _a; const isAuthorizationHeader = ({ key }) => key === 'Authorization'; const authorizationHeader = (_a = originalHeaders.find(isAuthorizationHeader)) === null || _a === void 0 ? void 0 : _a.value; // It is a business logic decision to remove the "Authorization" header. // If you think about it, this makes sense because if you've used Insomnia to fill out an Authorization form (e.g. Basic Auth), you wouldn't then also want the header to be added separately. // If users want to manually set up these headers they still aboslutely can, of course, but we try to keep thigns simple and help users out. const headers = (0, ramda_1.reject)(isAuthorizationHeader, originalHeaders); if (!authentication) { if (authorizationHeader) { switch (authorizationHeader === null || authorizationHeader === void 0 ? void 0 : authorizationHeader.substring(0, authorizationHeader.indexOf(' '))) { case 'Bearer': // will work for OAuth2 as well return { authentication: this.importBearerAuthenticationFromHeader(authorizationHeader), headers, }; case 'Basic': return { authentication: this.importBasicAuthenticationFromHeader(authorizationHeader), headers, }; case 'AWS4-HMAC-SHA256': return this.importАwsv4AuthenticationFromHeader(authorizationHeader, headers); case 'Digest': return { authentication: this.importDigestAuthenticationFromHeader(authorizationHeader), headers, }; case 'OAuth': return { authentication: this.importOauth1AuthenticationFromHeader(authorizationHeader), headers, }; default: return { authentication: {}, headers, }; } } return { authentication: {}, headers, }; } switch (authentication.type) { case 'awsv4': return { authentication: this.importAwsV4Authentication(authentication), headers, }; case 'basic': return { authentication: this.importBasicAuthentication(authentication), headers, }; case 'bearer': return { authentication: this.importBearerTokenAuthentication(authentication), headers, }; case 'digest': return { authentication: this.importDigestAuthentication(authentication), headers, }; case 'oauth1': return { authentication: this.importOauth1Authentication(authentication), headers, }; case 'oauth2': return { authentication: this.importOauth2Authentication(authentication), headers, }; default: return { authentication: {}, headers: originalHeaders, }; } }; this.importAwsV4Authentication = (auth) => { if (!auth.awsv4) { return {}; } const item = { type: 'iam', disabled: false, accessKeyId: 'aws-access-key', region: 'aws-region', secretAccessKey: 'aws-secret-key', service: 'aws-service-name', sessionToken: 'aws-session-token', }; const { schema } = this.collection.info; if (schema === POSTMAN_SCHEMA_V2_0) { const awsv4 = auth.awsv4; item.accessKeyId = awsv4 === null || awsv4 === void 0 ? void 0 : awsv4.accessKey; item.region = awsv4 === null || awsv4 === void 0 ? void 0 : awsv4.region; item.secretAccessKey = awsv4 === null || awsv4 === void 0 ? void 0 : awsv4.secretKey; item.service = awsv4 === null || awsv4 === void 0 ? void 0 : awsv4.service; item.sessionToken = awsv4 === null || awsv4 === void 0 ? void 0 : awsv4.sessionToken; } if (schema === POSTMAN_SCHEMA_V2_1) { const awsv4 = auth.awsv4; item.accessKeyId = this.findValueByKey(awsv4, 'accessKey'); item.region = this.findValueByKey(awsv4, 'region'); item.secretAccessKey = this.findValueByKey(awsv4, 'secretKey'); item.service = this.findValueByKey(awsv4, 'service'); item.sessionToken = this.findValueByKey(awsv4, 'sessionToken'); } return item; }; /** * example of AWS header: * @example AWS4-HMAC-SHA256 Credential=<accessKeyId>/20220110/<region>/<service>/aws4_request, SignedHeaders=accept;content-type;host;x-amz-date;x-amz-security-token, Signature=ed270ed6ad1cad3513f6edad9692e4496e321e44954c70a86504eea5e0ef1ff5 */ this.importАwsv4AuthenticationFromHeader = (authHeader, headers) => { var _a, _b; if (!authHeader) { return { authentication: {}, headers, }; } const isAMZSecurityTokenHeader = ({ key }) => key === 'X-Amz-Security-Token'; const sessionToken = (_a = headers === null || headers === void 0 ? void 0 : headers.find(isAMZSecurityTokenHeader)) === null || _a === void 0 ? void 0 : _a.value; const credentials = (_b = RegExp(/(?<=Credential=).*/).exec(authHeader)) === null || _b === void 0 ? void 0 : _b[0].split('/'); return { authentication: { type: 'iam', disabled: false, accessKeyId: credentials === null || credentials === void 0 ? void 0 : credentials[0], region: credentials === null || credentials === void 0 ? void 0 : credentials[2], secretAccessKey: '', service: credentials === null || credentials === void 0 ? void 0 : credentials[3], ...(sessionToken ? { sessionToken } : {}), }, headers: (0, ramda_1.reject)(isAMZSecurityTokenHeader, headers), }; }; this.importBasicAuthentication = (auth) => { if (!auth.basic) { return {}; } const item = { type: 'basic', disabled: false, username: '', password: '', }; const { schema } = this.collection.info; if (schema === POSTMAN_SCHEMA_V2_0) { const basic = auth.basic; item.username = basic === null || basic === void 0 ? void 0 : basic.username; item.password = basic === null || basic === void 0 ? void 0 : basic.password; } if (schema === POSTMAN_SCHEMA_V2_1) { const basic = auth.basic; item.username = this.findValueByKey(basic, 'username'); item.password = this.findValueByKey(basic, 'password'); } return item; }; this.importBasicAuthenticationFromHeader = (authHeader) => { var _a, _b; if (!authHeader) { return {}; } const authStringIndex = authHeader.trim().replace(/\s+/g, ' ').indexOf(' '); const hasEncodedAuthString = authStringIndex !== -1; const encodedAuthString = hasEncodedAuthString ? authHeader.substring(authStringIndex + 1) : ''; const authString = Buffer.from(encodedAuthString, 'base64').toString(); const item = { type: 'basic', disabled: false, username: (_a = RegExp(/.+?(?=\:)/).exec(authString)) === null || _a === void 0 ? void 0 : _a[0], password: (_b = RegExp(/(?<=\:).*/).exec(authString)) === null || _b === void 0 ? void 0 : _b[0], }; return item; }; this.importBearerTokenAuthentication = (auth) => { var _a; if (!auth.bearer) { return {}; } const item = { type: 'bearer', disabled: false, token: '', prefix: '', }; const { schema } = this.collection.info; if (schema === POSTMAN_SCHEMA_V2_0) { item.token = (_a = auth.bearer) === null || _a === void 0 ? void 0 : _a.token; } if (schema === POSTMAN_SCHEMA_V2_1) { item.token = this.findValueByKey(auth.bearer, 'token'); } return item; }; this.importBearerAuthenticationFromHeader = (authHeader) => { if (!authHeader) { return {}; } const authHeader2 = authHeader.replace(/\s+/, ' '); const tokenIndex = authHeader.indexOf(' '); return { type: 'bearer', disabled: false, token: tokenIndex + 1 ? authHeader2.substring(tokenIndex + 1) : '', prefix: '', }; }; this.importDigestAuthentication = (auth) => { if (!auth.digest) { return {}; } const item = { type: 'digest', disabled: false, username: '', password: '', }; const { schema } = this.collection.info; if (schema === POSTMAN_SCHEMA_V2_0) { const digest = auth.digest; item.username = digest === null || digest === void 0 ? void 0 : digest.username; item.password = digest === null || digest === void 0 ? void 0 : digest.password; } if (schema === POSTMAN_SCHEMA_V2_1) { const digest = auth.digest; item.username = this.findValueByKey(digest, 'username'); item.password = this.findValueByKey(digest, 'password'); } return item; }; // example: Digest username="Username", realm="Realm", nonce="Nonce", uri="//api/v1/report?start_date_min=2019-01-01T00%3A00%3A00%2B00%3A00&start_date_max=2019-01-01T23%3A59%3A59%2B00%3A00&projects[]=%2Fprojects%2F1&include_child_projects=1&search_query=meeting&columns[]=project&include_project_data=1&sort[]=-duration", algorithm="MD5", response="f3f762321e158aefe103529eda4ddb7c", opaque="Opaque" this.importDigestAuthenticationFromHeader = (authHeader) => { var _a; const item = { type: 'digest', disabled: false, username: (_a = RegExp(/(?<=username=")(.*?)(?=")/).exec(authHeader)) === null || _a === void 0 ? void 0 : _a[0], password: '', }; return item; }; this.importOauth1Authentication = (auth) => { if (!auth.oauth1) { return {}; } const item = { type: 'oauth1', disabled: false, callback: '', consumerKey: '', consumerSecret: '', nonce: '', privateKey: '', realm: '', signatureMethod: '', timestamp: '', tokenKey: '', tokenSecret: '', verifier: '', version: '', }; const { schema } = this.collection.info; if (schema === POSTMAN_SCHEMA_V2_0) { const oauth1 = auth.oauth1; item.consumerKey = oauth1 === null || oauth1 === void 0 ? void 0 : oauth1.consumerKey; item.consumerSecret = oauth1 === null || oauth1 === void 0 ? void 0 : oauth1.consumerSecret; item.nonce = oauth1 === null || oauth1 === void 0 ? void 0 : oauth1.nonce; item.realm = oauth1 === null || oauth1 === void 0 ? void 0 : oauth1.realm; item.signatureMethod = oauth1 === null || oauth1 === void 0 ? void 0 : oauth1.signatureMethod; item.timestamp = oauth1 === null || oauth1 === void 0 ? void 0 : oauth1.timestamp; item.tokenKey = oauth1 === null || oauth1 === void 0 ? void 0 : oauth1.token; item.tokenSecret = oauth1 === null || oauth1 === void 0 ? void 0 : oauth1.tokenSecret; item.version = oauth1 === null || oauth1 === void 0 ? void 0 : oauth1.version; } if (schema === POSTMAN_SCHEMA_V2_1) { const oauth1 = auth.oauth1; item.consumerKey = this.findValueByKey(oauth1, 'consumerKey'); item.consumerSecret = this.findValueByKey(oauth1, 'consumerSecret'); item.nonce = this.findValueByKey(oauth1, 'nonce'); item.realm = this.findValueByKey(oauth1, 'realm'); item.signatureMethod = this.findValueByKey(oauth1, 'signatureMethod'); item.timestamp = this.findValueByKey(oauth1, 'timestamp'); item.tokenKey = this.findValueByKey(oauth1, 'token'); item.tokenSecret = this.findValueByKey(oauth1, 'tokenSecret'); item.version = this.findValueByKey(oauth1, 'version'); } return item; }; // Example: OAuth realm="Realm",oauth_consumer_key="Consumer%20Key",oauth_token="Access%20Token",oauth_signature_method="HMAC-SHA1",oauth_timestamp="Timestamp",oauth_nonce="Nonce",oauth_version="Version",oauth_callback="Callback%20URL",oauth_verifier="Verifier",oauth_signature="TwJvZVasVWTL6X%2Bz3lmuiyvaX2Q%3D" this.importOauth1AuthenticationFromHeader = (authHeader) => { var _a, _b, _c, _d, _e, _f, _g, _h, _j; const item = { type: 'oauth1', disabled: false, callback: (_a = RegExp(/(?<=oauth_callback=")(.*?)(?=")/).exec(authHeader)) === null || _a === void 0 ? void 0 : _a[0], consumerKey: (_b = RegExp(/(?<=oauth_consumer_key=")(.*?)(?=")/).exec(authHeader)) === null || _b === void 0 ? void 0 : _b[0], consumerSecret: '', nonce: (_c = RegExp(/(?<=oauth_nonce=")(.*?)(?=")/).exec(authHeader)) === null || _c === void 0 ? void 0 : _c[0], privateKey: '', realm: (_d = RegExp(/(?<=realm=")(.*?)(?=")/).exec(authHeader)) === null || _d === void 0 ? void 0 : _d[0], signatureMethod: (_e = RegExp(/(?<=oauth_signature_method=")(.*?)(?=")/).exec(authHeader)) === null || _e === void 0 ? void 0 : _e[0], timestamp: (_f = RegExp(/(?<=oauth_timestamp=")(.*?)(?=")/).exec(authHeader)) === null || _f === void 0 ? void 0 : _f[0], tokenKey: (_g = RegExp(/(?<=oauth_token=")(.*?)(?=")/).exec(authHeader)) === null || _g === void 0 ? void 0 : _g[0], tokenSecret: '', verifier: (_h = RegExp(/(?<=oauth_verifier=")(.*?)(?=")/).exec(authHeader)) === null || _h === void 0 ? void 0 : _h[0], version: (_j = RegExp(/(?<=oauth_version=")(.*?)(?=")/).exec(authHeader)) === null || _j === void 0 ? void 0 : _j[0], }; return item; }; this.importOauth2Authentication = (auth) => { if (!auth.oauth2) { return {}; } const { schema } = this.collection.info; // Workaround for https://github.com/Kong/insomnia/issues/4437 // Note: We only support importing OAuth2 configuration from Postman v2.1 if (schema === POSTMAN_SCHEMA_V2_1) { const oauth2 = auth.oauth2; return { type: 'oauth2', disabled: false, accessTokenUrl: this.findValueByKey(oauth2, 'accessTokenUrl'), authorizationUrl: this.findValueByKey(oauth2, 'authUrl'), grantType: this.findValueByKey(oauth2, 'grant_type'), password: '', username: '', clientId: this.findValueByKey(oauth2, 'clientId'), clientSecret: this.findValueByKey(oauth2, 'clientSecret'), redirectUrl: this.findValueByKey(oauth2, 'redirect_uri'), }; } const item = { type: 'oauth2', disabled: true, accessTokenUrl: '', authorizationUrl: '', grantType: 'authorization_code', password: '', username: '', }; return item; }; this.findValueByKey = (array, key) => { if (!array) { return ''; } const obj = array.find(o => o.key === key); if (obj && typeof obj.value === 'string') { return obj.value || ''; } return ''; }; this.collection = collection; } } exports.ImportPostman = ImportPostman; const convert = rawData => { requestCount = 1; requestGroupCount = 1; try { const collection = JSON.parse(rawData); if (collection.info.schema === POSTMAN_SCHEMA_V2_0 || collection.info.schema === POSTMAN_SCHEMA_V2_1) { return new ImportPostman(collection).importCollection(); } } catch (error) { // Nothing } return null; }; exports.convert = convert; //# sourceMappingURL=postman.js.map