Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@lion/ajax

Package Overview
Dependencies
Maintainers
2
Versions
82
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@lion/ajax - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

dist-types/src/Ajax.d.ts

4

docs/use-cases.md

@@ -5,5 +5,5 @@ # Tools >> Ajax >> Use Cases ||20

import { html } from '@mdjs/mdjs-preview';
import { renderLitAsNode } from '@lion/helpers';
import { renderLitAsNode } from '@lion/ui/helpers.js';
import { ajax, createCacheInterceptors } from '@lion/ajax';
import '@lion/helpers/define';
import '@lion/ui/define-helpers/sb-action-logger.js';

@@ -10,0 +10,0 @@ const getCacheIdentifier = () => {

{
"name": "@lion/ajax",
"version": "1.0.0",
"version": "1.1.0",
"description": "Thin wrapper around fetch with support for interceptors.",

@@ -14,14 +14,16 @@ "license": "MIT",

"type": "module",
"main": "index.js",
"module": "index.js",
"exports": {
".": {
"types": "./dist-types/src/index.d.ts",
"default": "./src/index.js"
},
"./docs/*": "./docs/*"
},
"main": "src/index.js",
"module": "src/index.js",
"files": [
"*.d.ts",
"*.js",
"custom-elements.json",
"dist-types",
"docs",
"src",
"test",
"test-helpers",
"translations",
"types"
"test"
],

@@ -33,4 +35,5 @@ "scripts": {

"publish-docs": "node ../../packages-node/publish-docs/src/cli.js --github-url https://github.com/ing-bank/lion/ --git-root-dir ../../ --copy-dir docs/fundamentals/tools/ajax/assets",
"prepublishOnly": "npm run publish-docs",
"test": "cd ../../ && npm run test:browser -- --group ajax"
"prepublishOnly": "npm run types && npm run publish-docs",
"test": "cd ../../ && npm run test:browser -- --group ajax",
"types": "wireit"
},

@@ -41,4 +44,3 @@ "keywords": [

"http",
"lion",
"web-components"
"lion"
],

@@ -48,6 +50,16 @@ "publishConfig": {

},
"exports": {
".": "./index.js",
"./docs/*": "./docs/*"
"wireit": {
"types": {
"command": "tsc --build --pretty",
"files": [
"src/**/*.js",
"test/**/*.js",
"types/**",
"tsconfig.json"
],
"output": [
"dist-types/**"
]
}
}
}

@@ -8,5 +8,16 @@ /* eslint-disable consistent-return */

import { AjaxFetchError } from './AjaxFetchError.js';
import './typedef.js';
/**
* @typedef {import('../types/types.js').RequestInterceptor} RequestInterceptor
* @typedef {import('../types/types.js').CachedRequestInterceptor} CachedRequestInterceptor
* @typedef {import('../types/types.js').ResponseInterceptor} ResponseInterceptor
* @typedef {import('../types/types.js').CachedResponseInterceptor} CachedResponseInterceptor
* @typedef {import('../types/types.js').AjaxConfig} AjaxConfig
* @typedef {import('../types/types.js').CacheRequest} CacheRequest
* @typedef {import('../types/types.js').CacheResponse} CacheResponse
* @typedef {import('../types/types.js').CacheRequestExtension} CacheRequestExtension
* @typedef {import('../types/types.js').LionRequestInit} LionRequestInit
*/
/**
* @param {Response} response

@@ -184,10 +195,15 @@ * @returns {boolean}

try {
return {
response,
body: JSON.parse(responseText),
};
} catch (error) {
throw new Error(`Failed to parse response from ${response.url} as JSON.`);
/** @type {any} */
let body = responseText;
if (response.headers.get('content-type')?.includes('application/json')) {
try {
body = JSON.parse(responseText);
} catch (error) {
throw new Error(`Failed to parse response from ${response.url} as JSON.`);
}
} else {
body = responseText;
}
return { response, body };
}

@@ -225,3 +241,3 @@

// eslint-disable-next-line no-await-in-loop
interceptedResponse = await intercept(/** @type CacheResponse */ (interceptedResponse));
interceptedResponse = await intercept(/** @type {CacheResponse} */ (interceptedResponse));
}

@@ -228,0 +244,0 @@ return interceptedResponse;

@@ -1,2 +0,5 @@

import './typedef.js';
/**
* @typedef {import('../types/types.js').CachedRequests} CachedRequests
* @typedef {import('../types/types.js').CacheResponse} CacheResponse
*/

@@ -6,3 +9,3 @@ export default class Cache {

/**
* @type CachedRequests
* @type {CachedRequests}
* @private

@@ -9,0 +12,0 @@ */

@@ -1,2 +0,1 @@

import './typedef.js';
import Cache from './Cache.js';

@@ -6,2 +5,8 @@ import PendingRequestStore from './PendingRequestStore.js';

/**
* @typedef {import('../types/types.js').CacheRequest} CacheRequest
* @typedef {import('../types/types.js').CacheOptions} CacheOptions
* @typedef {import('../types/types.js').ValidatedCacheOptions} ValidatedCacheOptions
*/
/**
* The id for the cache session

@@ -50,3 +55,3 @@ * @type {string | undefined}

* Stringify URL search params
* @param {Params} params query string parameters object
* @param {*} params query string parameters object
* @returns {string} of querystring parameters WITHOUT `?` or empty string ''

@@ -157,4 +162,4 @@ */

*
* @param requestId { string }
* @param cacheOptions { CacheOptions }
* @param {string} requestId
* @param {CacheOptions} cacheOptions
*/

@@ -161,0 +166,0 @@ export const invalidateMatchingCache = (requestId, { invalidateUrls, invalidateUrlsRegex }) => {

@@ -1,7 +0,5 @@

import '../typedef.js';
/**
* Transforms a request, adding an accept-language header with the current application's locale
* if it has not already been set.
* @type {RequestInterceptor}
* @param {Request} request
*/

@@ -8,0 +6,0 @@ export async function acceptLanguageRequestInterceptor(request) {

/* eslint-disable no-param-reassign */
import '../typedef.js';
import {

@@ -14,2 +13,10 @@ ajaxCache,

/**
* @typedef {import('../../types/types.js').RequestInterceptor} RequestInterceptor
* @typedef {import('../../types/types.js').ResponseInterceptor} ResponseInterceptor
* @typedef {import('../../types/types.js').CacheOptions} CacheOptions
* @typedef {import('../../types/types.js').CacheRequest} CacheRequest
* @typedef {import('../../types/types.js').CacheResponse} CacheResponse
*/
/**
* Tests whether the request method is supported according to the `cacheOptions`

@@ -16,0 +23,0 @@ * @param {string[]} methods

@@ -1,2 +0,4 @@

import '../typedef.js';
/**
* @typedef {import('../../types/types.js').RequestInterceptor} RequestInterceptor
*/

@@ -23,3 +25,3 @@ /**

/**
* @type {RequestInterceptor}
* @param {Request} request
*/

@@ -26,0 +28,0 @@ async function xsrfRequestInterceptor(request) {

@@ -1,3 +0,1 @@

import './typedef.js';
export default class PendingRequestStore {

@@ -4,0 +2,0 @@ constructor() {

@@ -72,3 +72,4 @@ import { expect } from '@open-wc/testing';

// When
// @ts-expect-error
// TODO: fix AjaxConfig types => e.g. create FullAjaxConfig with everything "mandatory" and then AjaxConfig (= Partial of it) for user
// @ts-ignore
const ajax1 = new Ajax(config);

@@ -163,2 +164,8 @@ const defaultCacheIdentifierFunction = ajax1.options?.cacheOptions?.getCacheIdentifier;

it('handles non-json responses', async () => {
fetchStub.returns(Promise.resolve(new Response('!@#$')));
const response = await ajax.fetchJson('/foo');
expect(response.body).to.eql('!@#$');
});
describe('given a request body', () => {

@@ -181,3 +188,3 @@ it('encodes the request body as json', async () => {

const localAjax = new Ajax({ jsonPrefix: '//.,!' });
fetchStub.returns(Promise.resolve(new Response('//.,!{"a":1,"b":2}')));
fetchStub.returns(Promise.resolve(new Response('//.,!{"a":1,"b":2}', responseInit())));
const response = await localAjax.fetchJson('/foo');

@@ -189,3 +196,3 @@ expect(response.body).to.eql({ a: 1, b: 2 });

it('throws on invalid JSON responses', async () => {
fetchStub.returns(Promise.resolve(new Response('invalid-json')));
fetchStub.returns(Promise.resolve(new Response('invalid-json', responseInit())));

@@ -268,3 +275,5 @@ let thrown = false;

const response = /** @type {CacheResponse} */ (await await ajax.fetch('/foo'));
const response = /** @type {import('../types/types.js').CacheResponse} */ (
await await ajax.fetch('/foo')
);
expect(response.request).to.be.an.instanceOf(Request);

@@ -271,0 +280,0 @@ });

import { expect } from '@open-wc/testing';
import * as sinon from 'sinon';
import '../../src/typedef.js';
import { Ajax } from '../../index.js';
import { Ajax, createCacheInterceptors } from '@lion/ajax';
// TODO: these are private API? should they be exposed? if not why do we test them?
import { extendCacheOptions, resetCacheSession, ajaxCache } from '../../src/cacheManager.js';
import { createCacheInterceptors } from '../../src/interceptors/cacheInterceptors.js';

@@ -15,2 +15,7 @@ const MOCK_RESPONSE = 'mock response';

/**
* @typedef {import('../../types/types.js').CacheOptions} CacheOptions
* @typedef {import('../../types/types.js').RequestIdFunction} RequestIdFunction
*/
describe('cache interceptors', () => {

@@ -17,0 +22,0 @@ /**

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc