How to use @stoplight/json-ref-resolver - 9 common examples

To help you get started, we’ve selected a few @stoplight/json-ref-resolver examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github stoplightio / spectral / src / resolvers / http.ts View on Github external
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,
  },
});
github stoplightio / prism / packages / http / src / resolvers / http-and-file.ts View on Github external
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);
          });
        });
      },
    },
  },
github stoplightio / prism / packages / http / src / resolvers / http.ts View on Github external
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,
  },
});
github stoplightio / spectral / src / resolvers / http-and-file.ts View on Github external
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);
            }
          });
        });
      },
github box / box-openapi / src / specLoader.js View on Github external
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}`) }
      }
    }
github stoplightio / spectral / src / rulesets / reader.ts View on Github external
export async function readRuleset(uris: string | string[], opts?: IRulesetReadOptions): Promise {
  const base: IRuleset = {
    rules: {},
    functions: {},
  };

  const processedRulesets = new Set();
  const processRuleset = createRulesetProcessor(processedRulesets, new Cache(), opts);

  for (const uri of Array.isArray(uris) ? new Set([...uris]) : [uris]) {
    processedRulesets.clear(); // makes sure each separate ruleset starts with clear list
    const resolvedRuleset = await processRuleset(uri, uri);
    if (resolvedRuleset === null) continue;
    Object.assign(base.rules, resolvedRuleset.rules);
    Object.assign(base.functions, resolvedRuleset.functions);
  }

  return base;
}
github stoplightio / prism / packages / http / src / getHttpOperations.ts View on Github external
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 {
github stoplightio / spectral / src / spectral.ts View on Github external
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);
    }
  }
github fponticelli / tempo / tools / dom_generator / src / loader.ts View on Github external
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
}

@stoplight/json-ref-resolver

Recursively resolve JSON pointers and remote authorities.

Apache-2.0
Latest version published 9 months ago

Package Health Score

56 / 100
Full package analysis