generated from Alorel/basic-library-template-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
367 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,7 +48,6 @@ jobs: | |
- ubuntu-latest | ||
node: | ||
- 14.x | ||
- 12.x | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import {expect} from 'chai'; | ||
import type {Mockttp} from 'mockttp'; | ||
import {RSP_HEADERS, setUpMockServer} from '../test-util/MockServer'; | ||
import {testAjaxOpts} from '../test-util/testAjax'; | ||
import type {UnprocessedResponse} from '../types/Response'; | ||
import {makeRequest} from './makeRequest'; | ||
|
||
/* eslint-disable @typescript-eslint/no-magic-numbers */ | ||
|
||
describe('makeRequest', function () { | ||
|
||
describe('Successful response', () => { | ||
let server: Mockttp; | ||
let url: string; | ||
let rsp: UnprocessedResponse<any>; | ||
|
||
setUpMockServer(true, v => (server = v)); | ||
before(async () => { | ||
await server.get('/success').thenJson(200, {su: 'ccess'}, RSP_HEADERS); | ||
url = server.urlFor('/success'); | ||
rsp = await makeRequest<any>({...testAjaxOpts, preAsync: [], url}).toPromise(); | ||
}); | ||
|
||
it('Ok should be true', () => { | ||
expect(rsp.ok).to.eq(true); | ||
}); | ||
|
||
it('Original request should contain custom props', () => { | ||
expect(rsp.response.request).to.haveOwnProperty('preAsync'); | ||
}); | ||
|
||
it('Should have json response', () => { | ||
expect(rsp.response.response).to.deep.eq({su: 'ccess'}); | ||
}); | ||
}); | ||
|
||
describe('Error response', () => { | ||
let server: Mockttp; | ||
let url: string; | ||
let rsp: UnprocessedResponse<any>; | ||
|
||
setUpMockServer(true, v => (server = v)); | ||
before(async () => { | ||
await server.get('/fail').thenJson(420, {fai: 'lure'}, RSP_HEADERS); | ||
url = server.urlFor('/fail'); | ||
rsp = await makeRequest<any>({...testAjaxOpts, postAsync: [], url}).toPromise(); | ||
}); | ||
|
||
it('Ok should be true', () => { | ||
expect(rsp.ok).to.eq(false); | ||
}); | ||
|
||
it('Original request should contain custom props', () => { | ||
expect(rsp.response.request).to.haveOwnProperty('postAsync'); | ||
}); | ||
|
||
it('Should have json response', () => { | ||
expect(rsp.response.response).to.deep.eq({fai: 'lure'}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import {expect} from 'chai'; | ||
import {identity, noop, of} from 'rxjs'; | ||
import type {RxAjaxOptions as Opt} from '../types/Options'; | ||
import {merge} from './merge'; | ||
|
||
/* eslint-disable @typescript-eslint/no-magic-numbers */ | ||
|
||
describe('merge', function () { | ||
describe('headers', () => { | ||
it('Should omit if absent in both defaults and custom', () => { | ||
const d: Opt = {url: '/foo'}; | ||
const c: Opt = {withCredentials: true}; | ||
expect(merge(d, c)).to.deep.eq({url: '/foo', withCredentials: true}); | ||
}); | ||
|
||
it('Should merge if it has custom headers', () => { | ||
const d: Opt = {headers: {foo: 'bar'}}; | ||
const c: Opt = {headers: {qux: 'baz'}}; | ||
expect(merge(d, c)).to.deep.eq({headers: {foo: 'bar', qux: 'baz'}}); | ||
}); | ||
|
||
it('Shouldn\'t change reference of using defaults', () => { | ||
const d: Opt = {headers: {}}; | ||
const c: Opt = {url: '/'}; | ||
expect(merge(d, c).headers).to.eq(d.headers); | ||
}); | ||
}); | ||
|
||
describe('arrays', () => { | ||
it('Should omit if absent on both', () => { | ||
expect(merge({}, {})).to.deep.eq({}); | ||
}); | ||
|
||
it('Should reuse default instance', () => { | ||
const d: Opt = {pre: [identity]}; | ||
expect(merge(d, {}).pre).to.eq(d.pre); | ||
}); | ||
|
||
it('Should reuse custom instance', () => { | ||
const d: Opt = {post: [identity as any]}; | ||
expect(merge({}, d).post).to.eq(d.post); | ||
}); | ||
|
||
it('Should concat and dedupe', () => { | ||
const d: Opt = {postAsync: [identity, noop] as any[]}; | ||
const c: Opt = {postAsync: [noop, of] as any[]}; | ||
const {postAsync} = merge(d, c); | ||
|
||
expect(postAsync).to.have.lengthOf(3, 'length'); | ||
expect(postAsync![0]).to.eq(identity, '[identity]'); | ||
expect(postAsync![1]).to.eq(noop, '[noop]'); | ||
expect(postAsync![2]).to.eq(of, '[of]'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,16 @@ | ||
import type {MonoTypeOperatorFunction as MonoOp, Observable} from 'rxjs'; | ||
import {from, of} from 'rxjs'; | ||
import {last, switchMap} from 'rxjs/operators'; | ||
import type {MonoTypeOperatorFunction as MonoOp} from 'rxjs'; | ||
import {pipe} from 'rxjs'; | ||
import {switchMap} from 'rxjs/operators'; | ||
import type {RxAjaxOptions} from '../../types/Options'; | ||
import type {RxAjaxPostAsyncHook} from '../../types/Options'; | ||
import type {UnprocessedResponse} from '../../types/Response'; | ||
|
||
/** @internal */ | ||
export function applyPostAsyncMiddleware( | ||
idx: number, | ||
middleware: Required<RxAjaxOptions>['postAsync'] | ||
): MonoOp<UnprocessedResponse<any>> { | ||
return switchMap( | ||
function applyPostMiddlewareExecutor(response: UnprocessedResponse<any>): Observable<UnprocessedResponse<any>> { | ||
if (response.stop) { | ||
return of(response); | ||
} | ||
const pipes = (middleware as RxAjaxPostAsyncHook[]) | ||
.map((mid): MonoOp<UnprocessedResponse<any>> => switchMap(mid)); | ||
|
||
const pipes: Array<MonoOp<any>> = [last()]; | ||
if (middleware[idx + 1]) { | ||
pipes[1] = applyPostAsyncMiddleware(idx + 1, middleware); | ||
} | ||
|
||
const src$ = from(middleware[idx](response)); | ||
|
||
return src$.pipe.apply(src$, pipes) as Observable<UnprocessedResponse<any>>; // eslint-disable-line prefer-spread | ||
} | ||
); | ||
return pipe.apply(null, pipes as any[]) as MonoOp<UnprocessedResponse<any>>; // eslint-disable-line prefer-spread | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.