@algolia/requester-browser-xhr
Advanced tools
Comparing version
@@ -5,4 +5,2 @@ import { Requester } from '@algolia/client-common'; | ||
declare function echoRequester(status?: number): Requester; | ||
export { createXhrRequester, echoRequester }; | ||
export { createXhrRequester }; |
@@ -1,2 +0,2 @@ | ||
function c(){function r(t){return new Promise(s=>{let e=new XMLHttpRequest;e.open(t.method,t.url,!0),Object.keys(t.headers).forEach(n=>e.setRequestHeader(n,t.headers[n]));let i=(n,m)=>setTimeout(()=>{e.abort(),s({status:0,content:m,isTimedOut:!0})},n),u=i(t.connectTimeout,"Connection timeout"),o;e.onreadystatechange=()=>{e.readyState>e.OPENED&&o===void 0&&(clearTimeout(u),o=i(t.responseTimeout,"Socket timeout"))},e.onerror=()=>{e.status===0&&(clearTimeout(u),clearTimeout(o),s({content:e.responseText||"Network request failed",status:e.status,isTimedOut:!1}))},e.onload=()=>{clearTimeout(u),clearTimeout(o),s({content:e.responseText,status:e.status,isTimedOut:!1})},e.send(t.data)})}return{send:r}}import{createEchoRequester as a}from"@algolia/client-common";function p(r=200){return a({getURL:t=>new URL(t),status:r})}export{c as createXhrRequester,p as echoRequester}; | ||
function m(){function r(t){return new Promise(s=>{let e=new XMLHttpRequest;e.open(t.method,t.url,!0),Object.keys(t.headers).forEach(n=>e.setRequestHeader(n,t.headers[n]));let i=(n,a)=>setTimeout(()=>{e.abort(),s({status:0,content:a,isTimedOut:!0})},n),u=i(t.connectTimeout,"Connection timeout"),o;e.onreadystatechange=()=>{e.readyState>e.OPENED&&o===void 0&&(clearTimeout(u),o=i(t.responseTimeout,"Socket timeout"))},e.onerror=()=>{e.status===0&&(clearTimeout(u),clearTimeout(o),s({content:e.responseText||"Network request failed",status:e.status,isTimedOut:!1}))},e.onload=()=>{clearTimeout(u),clearTimeout(o),s({content:e.responseText,status:e.status,isTimedOut:!1})},e.send(t.data)})}return{send:r}}export{m as createXhrRequester}; | ||
//# sourceMappingURL=requester.xhr.js.map |
export * from './src/createXhrRequester'; | ||
export * from './src/echoRequester'; |
{ | ||
"name": "@algolia/requester-browser-xhr", | ||
"version": "5.3.2", | ||
"version": "5.4.0", | ||
"description": "Promise-based request library for browser using xhr.", | ||
@@ -29,20 +29,16 @@ "repository": { | ||
"clean": "rm -rf ./dist || true", | ||
"test": "jest", | ||
"test": "vitest --run", | ||
"test:bundle": "publint . && attw --pack . --ignore-rules cjs-resolves-to-esm" | ||
}, | ||
"dependencies": { | ||
"@algolia/client-common": "5.3.2" | ||
"@algolia/client-common": "5.4.0" | ||
}, | ||
"devDependencies": { | ||
"@arethetypeswrong/cli": "0.16.1", | ||
"@babel/preset-env": "7.25.4", | ||
"@babel/preset-typescript": "7.24.7", | ||
"@types/jest": "29.5.12", | ||
"@types/node": "22.5.1", | ||
"jest": "29.7.0", | ||
"jest-environment-jsdom": "29.7.0", | ||
"jsdom": "25.0.0", | ||
"publint": "0.2.10", | ||
"ts-jest": "29.2.5", | ||
"tsup": "8.2.4", | ||
"typescript": "5.5.4", | ||
"vitest": "2.0.5", | ||
"xhr-mock": "2.5.1" | ||
@@ -49,0 +45,0 @@ }, |
import type http from 'http'; | ||
import type { EndRequest } from '@algolia/client-common'; | ||
import { describe, test, beforeAll, afterAll, beforeEach, afterEach, expect } from 'vitest'; | ||
import type { MockRequest, MockResponse } from 'xhr-mock'; | ||
@@ -20,6 +21,10 @@ import mock from 'xhr-mock'; | ||
describe('status code handling', () => { | ||
beforeEach(() => mock.setup()); | ||
afterEach(() => mock.teardown()); | ||
beforeEach(() => { | ||
mock.setup(); | ||
}); | ||
afterEach(() => { | ||
mock.teardown(); | ||
}); | ||
it('sends requests', async () => { | ||
test('sends requests', async () => { | ||
mock.post(BASE_URL, (req: MockRequest, res: MockResponse): MockResponse => { | ||
@@ -36,3 +41,3 @@ expect(req.method()).toEqual('POST'); | ||
it('resolves status 200', async () => { | ||
test('resolves status 200', async () => { | ||
const body = getStringifiedBody(); | ||
@@ -52,3 +57,3 @@ | ||
it('resolves status 300', async () => { | ||
test('resolves status 300', async () => { | ||
const reason = 'Multiple Choices'; | ||
@@ -68,3 +73,3 @@ | ||
it('resolves status 400', async () => { | ||
test('resolves status 400', async () => { | ||
const body = getStringifiedBody({ | ||
@@ -86,3 +91,3 @@ message: 'Invalid Application-Id or API-Key', | ||
it('handles the protocol', async () => { | ||
test('handles the protocol', async () => { | ||
const body = getStringifiedBody(); | ||
@@ -115,7 +120,10 @@ | ||
afterAll((done) => { | ||
server.close(() => done()); | ||
}); | ||
afterAll( | ||
() => | ||
new Promise((done) => { | ||
done(); | ||
}), | ||
); | ||
it('connection timeouts with the given 1 seconds connection timeout', async () => { | ||
test('connection timeouts with the given 1 seconds connection timeout', async () => { | ||
const before = Date.now(); | ||
@@ -135,3 +143,3 @@ const response = await requester.send({ | ||
it('connection timeouts with the given 2 seconds connection timeout', async () => { | ||
test('connection timeouts with the given 2 seconds connection timeout', async () => { | ||
const before = Date.now(); | ||
@@ -151,3 +159,3 @@ const response = await requester.send({ | ||
it("socket timeouts if response don't appears before the timeout with 2 seconds timeout", async () => { | ||
test("socket timeouts if response don't appears before the timeout with 2 seconds timeout", async () => { | ||
const before = Date.now(); | ||
@@ -168,3 +176,3 @@ | ||
it("socket timeouts if response don't appears before the timeout with 3 seconds timeout", async () => { | ||
test("socket timeouts if response don't appears before the timeout with 3 seconds timeout", async () => { | ||
const before = Date.now(); | ||
@@ -185,3 +193,3 @@ | ||
it('do not timeouts if response appears before the timeout', async () => { | ||
test('do not timeouts if response appears before the timeout', async () => { | ||
const before = Date.now(); | ||
@@ -205,3 +213,3 @@ const response = await requester.send({ | ||
describe('error handling', () => { | ||
it('resolves dns not found', async () => { | ||
test('resolves dns not found', async () => { | ||
const request: EndRequest = { | ||
@@ -223,3 +231,3 @@ url: 'https://this-dont-exist.algolia.com', | ||
it('resolves general network errors', async () => { | ||
test('resolves general network errors', async () => { | ||
mock.post(BASE_URL, () => Promise.reject(new Error('This is a general error'))); | ||
@@ -226,0 +234,0 @@ |
Sorry, the diff of this file is not supported yet
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
8
-33.33%1
-50%13471
-6.84%7
-12.5%+ Added
- Removed
Updated