-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(): adds pagination service (#1)
* feat(): adds pagination service This adds pagination service to the got service in an attempt to immitate the original got package. * docs(pagination): updates readme documentation This updates the readme documentation with usage example for the pagination feature. Co-authored-by: B'Tunde Aromire <babatunde.aromire@onacrefund.org>
- Loading branch information
Showing
15 changed files
with
385 additions
and
57 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
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,9 @@ | ||
import { Got, InstanceDefaults } from 'got'; | ||
|
||
export abstract class AbstractService { | ||
readonly defaults: InstanceDefaults; | ||
|
||
constructor(protected readonly got: Got) { | ||
this.defaults = this.got.defaults; | ||
} | ||
} |
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 @@ | ||
export * from './rxjs'; |
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 @@ | ||
export * from './scheduleAsyncIterable'; |
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,27 @@ | ||
import { asapScheduler } from 'rxjs'; | ||
import { take } from 'rxjs/operators'; | ||
|
||
import { scheduledAsyncIterable } from './scheduleAsyncIterable'; | ||
|
||
describe('scheduleAsyncIterable()', () => { | ||
it('', () => { | ||
const iterator = async function* () { | ||
let i = 1; | ||
while (true) { | ||
yield i; | ||
i += 1; | ||
} | ||
}; | ||
|
||
const iterable = iterator(); | ||
let count = 1; | ||
|
||
scheduledAsyncIterable<number>(iterable, asapScheduler) | ||
.pipe(take(5)) | ||
.subscribe({ | ||
next(v) { | ||
expect(v).toEqual(count++); | ||
}, | ||
}); | ||
}); | ||
}); |
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,28 @@ | ||
import { Observable, Subscriber, Subscription, SchedulerLike } from 'rxjs'; | ||
|
||
export const scheduledAsyncIterable = <T = any>( | ||
input: AsyncIterable<T> | AsyncGenerator<T>, | ||
scheduler: SchedulerLike, | ||
): Observable<T> => { | ||
return new Observable<T>((subscriber: Subscriber<T>) => { | ||
const subscription = new Subscription(); | ||
subscription.add( | ||
scheduler.schedule(() => { | ||
const iterator = input[Symbol.asyncIterator](); | ||
subscription.add( | ||
scheduler.schedule(function () { | ||
iterator.next().then((result: IteratorResult<T>) => { | ||
if (result.done) { | ||
subscriber.complete(); | ||
} else { | ||
subscriber.next(result.value); | ||
this.schedule(); | ||
} | ||
}); | ||
}), | ||
); | ||
}), | ||
); | ||
return subscription; | ||
}); | ||
}; |
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
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,81 @@ | ||
import * as faker from 'faker'; | ||
import { Got, HTTPError, Response } from 'got'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
|
||
import { GotService } from './got.service'; | ||
import { GOT_INSTANCE } from './got.constant'; | ||
import { PaginationService } from './paginate.service'; | ||
|
||
describe('GotService', () => { | ||
let service: PaginationService; | ||
const gotInstance: Partial<Got> = { | ||
defaults: { | ||
options: jest.fn(), | ||
} as any, | ||
}, | ||
exemptedKeys = ['makeObservable', 'defaults', 'constructor']; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
PaginationService, | ||
{ | ||
provide: GOT_INSTANCE, | ||
useValue: gotInstance, | ||
}, | ||
{ | ||
provide: GotService, | ||
useValue: {}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
service = module.get<PaginationService>(PaginationService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
|
||
const methods = Object.getOwnPropertyNames( | ||
PaginationService.prototype, | ||
).filter(key => !~exemptedKeys.indexOf(key)); | ||
|
||
methods.forEach((key, index) => { | ||
it(`${key}()`, complete => { | ||
const result: Partial<Response> = { body: {} }; | ||
|
||
gotInstance[key] = jest.fn().mockResolvedValueOnce(result); | ||
|
||
service[key](faker.internet.url()).subscribe({ | ||
next(response) { | ||
expect(response).toBe(result); | ||
}, | ||
complete, | ||
}); | ||
}); | ||
|
||
if (methods.length - 2 === index) { | ||
it('check that defaults is set', () => | ||
expect('options' in service.defaults).toBe(true)); | ||
|
||
it('should get request', () => { | ||
service[key](faker.internet.url()); | ||
}); | ||
|
||
it('should check error reporting', () => { | ||
const result: any = { body: {}, statusCode: 400 }; | ||
|
||
gotInstance[key] = jest | ||
.fn() | ||
.mockRejectedValueOnce(new HTTPError(result)); | ||
|
||
service[key](faker.internet.url()).subscribe({ | ||
error(error) { | ||
expect(error).toBeInstanceOf(HTTPError); | ||
}, | ||
}); | ||
}); | ||
} | ||
}); | ||
}); |
Oops, something went wrong.