Détail du package

simple-get-json

brettz970MIT10.0.0

getJSON function similar to that of jQuery's for Node or the browser

json

readme

npm Dependencies devDependencies

eslint badge eslint 3rd party badge

testing badge coverage badge

Known Vulnerabilities Total Alerts Code Quality: Javascript

Filesize badge (UMD) Filesize badge (ESM) !--[![License](https://img.shields.io/npm/l/getJSON.svg)](LICENSE-MIT.txt)-- Licenses badge

(see also licenses for dev. deps.)

issuehunt-to-marktext

getJSON

getJSON function similar to that of jQuery's.

If no callback or error-back is provided (or when an array of URLs is supplied), a promise will be returned. If an array is provided along with a callback, that callback will be executed before the promise is resolved.

You may use the file index-es.js to use the ES2017 await keyword for Promise results, as well as take advantage of ES6 Module import:

try {
  const [urlObj1, urlObj2] = await getJSON([url1, url2]);
} catch (err) {
  // Handle errors here
}

simple-get-json also accepts an array of URLs (waiting for all to load):

const [obj1, obj2] = await getJSON([url1, url2]);
// Do something with "obj1" and "obj2"

Alternatively, you can use regular then Promises:

getJSON([url1, url2]).then(function (objsArr) {
  // Do something with "objsArr" array
}, function (err) {
  // Handle any errors here
  console.log('err', err);
});

Or use the old callback style.

getJSON(url, function (data) {
  // Do something with "data"
});

An optional third argument can be provided as an error-back (which will be supplied the error message and originally supplied URL).

Install

npm install simple-get-json

Setup

Browser

<!--
For older browser support
<script src="node_modules/core-js-bundle/minified.js"></script>
-->
<script src="node_modules/simple-get-json/dist/index.js"></script>
getJSON(...args);

or for ESM:

  1. Direct use:
import {getJSON} from './node_modules/simple-get-json/dist/index-es.js';
  1. With a bundler:
import {getJSON} from 'simple-get-json';

Node (CJS)

const {getJSON} = require('simple-get-json');

See below for buildGetJSON usage.

Node (ESM)

In ESM Node, you can:

  1. Directly import the module (for use relative to the current working directory):
import {getJSON} from 'simple-get-json';
  1. Build a version of getJSON which works relative to the current file (or some other URL):
import {buildGetJSON} from 'simple-get-json';

const getJSON = buildGetJSON({
  baseURL: import.meta.url
});

OR:

  1. Build a version of getJSON which works relative to a specific file directory:
import {buildGetJSON} from 'simple-get-json';

const getJSON = buildGetJSON({
  cwd: '/some/current/working/directory'
});

Todo

  • Support named parameters ala jQuery
  • Support rest of jQuery API
  • Make local or URL loading optional for Node

See also

changelog

CHANGES for simple-get-json

11.0.0 (unreleased)

  • BREAKING chore: require Node >= 20
  • chore: update devDeps.

10.0.0

  • feat: adds TypeScript
  • chore: update node-fetch, devDeps. and lint; require Node 18+

9.0.1

  • fix: service workers import issue

9.0.0

  • BREAKING feat: Avoid utilizing built-in fetch as uses URL-based loading only now in later Node versions

8.2.0

  • Dev security: Switch to server (http-server) without reported vulnerabilities
  • Fix: For .mjs file, avoid require
  • Linting: Per latest ash-nazg
  • npm: Update devDeps. including switching to latest ash-nazg peerDeps
  • npm: Switch to pnpm

8.1.1

  • Fix: For exports use proper relative path

8.1.0

  • npm: Bump node-fetch
  • Linting (ESLint): As per latest ash-nazg
  • Testing: Switch to stable version of mocha-multi-reporters
  • npm: Update devDeps; add missing eslint-formatter-badger devDep.

8.0.3

  • Fix: Worker compatibility

8.0.2

  • Fix: Resume with default export for UMD browser build

8.0.1

  • Build: Ensure transforming import for CJS

8.0.0

  • Breaking change: Make as proper native Node ESM module
  • Breaking change: Add back module to package.json
  • Breaking change: Rename again as polyglot (even if not supported by Rollup)
  • Enhancement: Add browser to package.json
  • Fix: ESM Rollup format wasn't being specified properly
  • Docs: Add badges
  • Linting: Check hidden files
  • Testing: Move to c8 for native Node coverage

7.0.2

  • Fix: For polyglot capability, export as UMD

7.0.1

  • Fix: At least make no-op so Node file can be polyglot

7.0.0

  • Breaking change: Make as named rather than default export
  • Breaking change: Remove polyglot file (in favor of browser or Node being auto-selected appropriately)
  • Breaking change: Point to different dist paths for main; remove browser in favor of exports.browser; add exports with CJS and MJS files
  • Breaking change: Indicate engines as 0.12.0 minimum (requires Promise)
  • Breaking change (Node): Fix bug with base directory, making it now by default resolve relative to process.cwd; Node version now also adds ability to read URLs
  • Build: Use "json" extension with RC file
  • Enhancement: Add buildGetJSON export for baking in base path or base URL (e.g., for use with import.meta.url)
  • Enhancement: Add getDirectoryForURL module (used by index-node)
  • Linting: As per latest ash-nazg/ESLint 7; apply sauron-node
  • Testing: Switch to default Mocha test directory; build tests with @babel/register
  • Testing: Switch to Mocha + Chai + Babel + nyc
  • npm: Avoid opening file in start script
  • npm: Avoid linting with rollup script
  • npm: Update rollup-plugin-babel to @rollup/plugin-babel and make explicit babelHelpers as bundled
  • npm: Update devDeps

6.0.0

  • npm: Update devDeps
  • yarn: Remove yarn.lock

5.0.1

  • npm: Update devDeps

5.0.0

  • Linting: Switch to eslint-config-ash-nazg; apply to HTML/Markdown JS
  • Build: Drop @babel/polyfill dep.
  • npm: Change deprecated opn-cli->open-cli
  • npm: Update devDeps

4.1.0

  • Build: Ensure has CommonJS build without need for polyfill/regenerator-runtime
  • Linting (Markdown): Fix .remarkrc format
  • npm: Update devDeps

4.0.0

  • Breaking change: Add ES distributed build and move distribution files to dist (and source files to src) and update package.json main, browser and module fields
  • Breaking change: Remove file for Bower (now that it is deprecated)
  • Breaking change: Change from babel-polyfill to @babel/polyfill
  • Linting (ESLint): Override "standard" for object-curly-spacing; avoid Node usage of pseudo-deprecated url.parse
  • Build: Avoid need for rollup-plugin-async
  • Docs: Add reference to @babel/polyfill
  • npm: Update to Babel7; rename "build" script to "rollup"; update other devDeps and local-xmlhttprequest dep

3.2.3

  • npm: Switch babel-polyfill to dependency to ensure present for inheriting projects

3.2.2

  • npm: Add babel-polyfill
  • npm: Update devDeps and update builds

3.2.1

  • Fix .npmignore

3.2.0

  • Build: Add yarn
  • npm: Use babel-preset-env instead of babel-env (former was also causing problems in Travis environment (and rebuilding .babelrc))
  • npm: Update dev deps

3.1.0

  • Build: Rename Node file to polyglot as is UMD
  • Enhancement (npm): Point to polyglot file from module property (workable in Node as well as browser)
  • Enhancement: Check for an undefined fetch rather than a defined module (should be safer in more environments)

3.0.3

  • Build: Fix for Node

3.0.2

  • Build: Fix for Node
  • npm: Add ESLint script

3.0.1

  • Build: Properly apply Babel

3.0.0

  • Breaking change: Assume fetch support or polyfill in browser
  • Refactoring: Build index.js (for Node or browser) from source (using Rollup with rollup.config.js, applying to tests for restoring Node testing support)

2.1.0

  • Use Rollup-friendly module tool.

2.0.0

  • Breaking change: Return error objects instead of strings in both files
  • Breaking change: Add URL to error message passed to errBack as well as to Promise catch
  • Enhancement: Within new index-es2017.js file, export as ES6 module, refactoring to leverage ES2017 await

1.1.0

  • Allow omission of callback when passing an array of URLs (can instead use Promises chain)

1.0.1

  • Throw errors properly when jsonURL is an array and errBack is not present

1.0.0

  • Added support for array of resources as first argument
  • Returns a Promise when the second and third arguments are not functions (or are absent)
  • Allow errBack for third argument
  • Requires Array.isArray, Function.prototype.bind, and Promise polyfills if using on older environments

0.3.2

  • Last initial version