Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import { Resolver } from '@stoplight/json-ref-resolver';
import request from '../request';
export const httpReader = {
async resolve(ref: any) {
return (await request(String(ref))).text();
},
};
// resolves http and https $refs, and internal $refs
export const httpResolver = new Resolver({
resolvers: {
https: httpReader,
http: httpReader,
},
});
import { Resolver } from '@stoplight/json-ref-resolver';
import { parse } from '@stoplight/yaml';
import * as fs from 'fs';
import { httpReader } from './http';
// resolves files, http and https $refs, and internal $refs
export const httpAndFileResolver = new Resolver({
resolvers: {
https: httpReader,
http: httpReader,
file: {
resolve(ref: any) {
return new Promise((resolve, reject) => {
const path = ref.path();
fs.readFile(path, 'utf8', (err, data) => {
if (err) reject(err);
resolve(data);
});
});
},
},
},
import { Resolver } from '@stoplight/json-ref-resolver';
import axios from 'axios';
export const httpReader = {
resolve(ref: unknown) {
return axios.get(String(ref)).then(d => d.data);
},
};
// resolves http and https $refs, and internal $refs
export const httpResolver = new Resolver({
resolvers: {
https: httpReader,
http: httpReader,
},
});
import { Resolver } from '@stoplight/json-ref-resolver';
import * as fs from 'fs';
import { httpReader } from './http';
// resolves files, http and https $refs, and internal $refs
export const httpAndFileResolver = new Resolver({
resolvers: {
https: httpReader,
http: httpReader,
file: {
resolve(ref: any) {
return new Promise((resolve, reject) => {
const path = ref.path();
fs.readFile(path, 'utf8', (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
},
const fs = require('fs-extra')
const { Resolver } = require("@stoplight/json-ref-resolver")
const yaml = require('js-yaml')
/**
* A specification resolver that is used by Spectral to
* resolve local file names
*
*/
const resolver = new Resolver({
resolvers: {
file: {
/**
* Translates custom URIs to objects
*/
resolve(uri) {
// get the content at the path
const path = uri.path()
const content = fs.readFileSync(`./content/${path}`)
// parse the path as yml or json if needed
if (path.endsWith('.json')) { return JSON.parse(content) }
else if (path.endsWith('.yml')) { return yaml.load(content) }
else if (path.endsWith('.md')) { return String(content) }
else { throw new Error(`Could not find reference ${path}`) }
}
}
import { transformOas2Operation, transformOas3Operation } from '@stoplight/http-spec';
import { Resolver } from '@stoplight/json-ref-resolver';
import { resolveFile, resolveHttp } from '@stoplight/ref-resolvers';
import { IHttpOperation } from '@stoplight/types';
import { parse } from '@stoplight/yaml';
import fetch from 'node-fetch';
import * as fs from 'fs';
import { flatten, get, keys, map, uniq } from 'lodash';
import { EOL } from 'os';
import { resolve } from 'path';
const httpAndFileResolver = new Resolver({
resolvers: {
https: { resolve: resolveHttp },
http: { resolve: resolveHttp },
file: { resolve: resolveFile },
},
parseResolveResult: opts => Promise.resolve({ ...opts, result: parse(opts.result) }),
});
export async function getHttpOperationsFromResource(file: string): Promise {
const isRemote = /^https?:\/\//i.test(file);
const fileContent = isRemote ? await fetch(file).then(d => d.text()) : fs.readFileSync(file, { encoding: 'utf8' });
return getHttpOperations(fileContent, isRemote ? file : resolve(file));
}
export default async function getHttpOperations(specContent: string, baseUri?: string): Promise {
constructor(opts?: IConstructorOpts) {
this._computeFingerprint = memoize(opts?.computeFingerprint || defaultComputeResultFingerprint);
this._resolver = opts && opts.resolver ? opts.resolver : new Resolver();
this.formats = {};
const cacheKey = this._resolver instanceof Resolver ? this._resolver.uriCache : this._resolver;
const _parsedRefs = Spectral._parsedCache.get(cacheKey);
if (_parsedRefs) {
this._parsedRefs = _parsedRefs;
} else {
this._parsedRefs = {};
Spectral._parsedCache.set(cacheKey, this._parsedRefs);
}
}
import { Resolver } from '@stoplight/json-ref-resolver'
import { safeLoad } from 'js-yaml'
import * as fs from 'fs'
const resolver = new Resolver()
export const loader = async (path: string) => {
const content = fs.readFileSync(path, 'utf8')
const yaml = safeLoad(content)
return (await resolver.resolve(yaml)).result
}