codeasashu / insomnia-oas-converter

Insomnia file to openapi converter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The sample code does not run

nirmdesai opened this issue · comments

I am simply trying to run the sample code provided with README, after installing the converter.

Here is the line with error:

let schema = new SchemaConventer(insomniaExportedInput, openapiConfig);

TypeError: SchemaConventer is not a constructor
    at Object.<anonymous> (/Users/nirmit.desai/github/convert.js:15:14)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
    at startup (internal/bootstrap/node.js:285:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)

@nermdesai Can you please mention the version you are using? It looks like you might be using an older version. Can you try with npm i insomnia-oas-converter@1.0.2?

@codeasashu: The above problem I reported was because I was running the sample code with node. I then realized it was typescript. So I installed typescript and ts-node packages. I simply put your sample code in convert.ts and run it in the same folder as ts-node convert.ts. I see this error:

Error: Cannot find module '@types/node/package.json'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:580:15)
    at Function.resolve (internal/modules/cjs/helpers.js:30:19)
    at typeDirectiveNames.map (/usr/local/lib/node_modules/ts-node/src/index.ts:746:52)
    at Array.map (<anonymous>)
    at Object.resolveTypeReferenceDirectives (/usr/local/lib/node_modules/ts-node/src/index.ts:734:33)
    at actualResolveTypeReferenceDirectiveNamesWorker (/usr/local/lib/node_modules/typescript/lib/typescript.js:109514:143)
    at resolveTypeReferenceDirectiveNamesWorker (/usr/local/lib/node_modules/typescript/lib/typescript.js:109773:26)
    at Object.createProgram (/usr/local/lib/node_modules/typescript/lib/typescript.js:109612:35)
    at synchronizeHostData (/usr/local/lib/node_modules/typescript/lib/typescript.js:153746:26)
    at Object.getProgram (/usr/local/lib/node_modules/typescript/lib/typescript.js:153878:13)

Do I need a certain project structure? I have not created any project here, just that one script that I am attaching here as well.
convert.ts

var SchemaConventer = require('insomnia-oas-converter');

// Your openapi spec config
let openapiConfig = {
    "title": "back-to-work",
    "description": "Web application APIs for touchless visitor experience",
    "version": "1.0.0",
    "baseUrl": "http://example.tld"
};

// Read insomnia exported (in v4 format) json file into a dict
let insomniaExportedInput = {....}

let schema = new SchemaConventer(insomniaExportedInput, openapiConfig);
let spec = schema.convert();

// Convert to yaml
// let spec_in_yaml = spec.as_yaml()

// Convert to json
// let spec_in_json = spec.as_json()

// Or you can simply get the spec is javascript object
let spec_dict = spec.as_dict();

var dictstring = JSON.stringify(spec_dict);

var fs = require('fs');
fs.writeFile("thing.json", dictstring, function(err, result) {
    if(err) console.log('error', err);
});

@nermdesai Actually you are close. Since the package is primarily intended for EJS format, to use it in node env, you simply have to require the default function. i.e.

do this:

var SchemaConventer = require('insomnia-oas-converter').default;

instead of this:

var SchemaConventer = require('insomnia-oas-converter');

It should work.

PS: You do not need ts-node to run this. Simply node convert.js should work (I renamed it to js)

Okay, seems to have made some progress, another error now:

Nirmits-MacBook-Pro-2:github nirmit.desai$ node convert.js
TypeError: variable.matchAll is not a function
    at trimTemplate (/Users/nirmit.desai/github/node_modules/insomnia-oas-converter/dist/insomnia-oas-converter.cjs.development.js:127:26)
    at urlFromPath (/Users/nirmit.desai/github/node_modules/insomnia-oas-converter/dist/insomnia-oas-converter.cjs.development.js:166:18)
    at OpenapiCollector._get_oas_pathparams (/Users/nirmit.desai/github/node_modules/insomnia-oas-converter/dist/insomnia-oas-converter.cjs.development.js:455:18)
    at OpenapiCollector.get_path_pathparams (/Users/nirmit.desai/github/node_modules/insomnia-oas-converter/dist/insomnia-oas-converter.cjs.development.js:278:25)
    at OpenapiCollector.addRequest (/Users/nirmit.desai/github/node_modules/insomnia-oas-converter/dist/insomnia-oas-converter.cjs.development.js:297:25)
    at SchemaConventer.convert (/Users/nirmit.desai/github/node_modules/insomnia-oas-converter/dist/insomnia-oas-converter.cjs.development.js:620:26)
    at Object.<anonymous> (/Users/nirmit.desai/github/convert.js:16:19)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
/Users/nirmit.desai/github/convert.js:25
let spec_dict = spec.as_dict();
                     ^

TypeError: Cannot read property 'as_dict' of null
    at Object.<anonymous> (/Users/nirmit.desai/github/convert.js:25:22)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
    at startup (internal/bootstrap/node.js:285:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)

@nermdesai This seems to be a bug. Is it possible for you to share the original request which is failing? Please hide (try not to remove) any secrets from the requests.

@codeasashu Insomnia_2021-06-24.json.zip
Attaching the input Insomnia json that I'm trying to convert to OpenAPI 3.0.