@droz-js/sdk
Advanced tools
Comparing version
{ | ||
"name": "@droz-js/sdk", | ||
"description": "Droz SDK", | ||
"version": "0.2.7", | ||
"version": "0.2.8", | ||
"private": false, | ||
@@ -6,0 +6,0 @@ "exports": { |
@@ -9,2 +9,12 @@ import type { ExecutionResult } from 'graphql'; | ||
export declare function mapGraphqlResponse<T>(response: ExecutionResult<T> | Error): T; | ||
export declare function mapAsyncIterableGraphqlResponse<T>(iterable: AsyncIterableIterator<ExecutionResult<T, any>>): AsyncGenerator<Awaited<T>, void, unknown>; | ||
declare class AsyncIterableIteratorMapper<T = any> implements AsyncIterableIterator<T> { | ||
private from; | ||
private mapper; | ||
constructor(from: AsyncIterableIterator<ExecutionResult<T, any>>, mapper: (response: ExecutionResult<T, any>) => T); | ||
next(...args: [] | [undefined]): Promise<IteratorResult<T, any>>; | ||
return(value?: any): Promise<IteratorResult<T, any>>; | ||
throw(e?: any): Promise<IteratorResult<T, any>>; | ||
[Symbol.asyncIterator](): AsyncIterableIterator<T>; | ||
} | ||
export declare function mapAsyncIterableGraphqlResponse<T>(iterable: AsyncIterableIterator<ExecutionResult<T, any>>): AsyncIterableIteratorMapper<T>; | ||
export {}; |
@@ -44,7 +44,37 @@ "use strict"; | ||
exports.mapGraphqlResponse = mapGraphqlResponse; | ||
async function* mapAsyncIterableGraphqlResponse(iterable) { | ||
for await (const each of iterable) { | ||
yield mapGraphqlResponse(each); | ||
class AsyncIterableIteratorMapper { | ||
from; | ||
mapper; | ||
constructor(from, mapper) { | ||
this.from = from; | ||
this.mapper = mapper; | ||
} | ||
async next(...args) { | ||
const next = await this.from.next(...args); | ||
return { | ||
done: next.done, | ||
value: this.mapper(next.value) | ||
}; | ||
} | ||
async return(value) { | ||
const ret = await this.from.return(value); | ||
return { | ||
done: ret.done, | ||
value: this.mapper(ret.value) | ||
}; | ||
} | ||
async throw(e) { | ||
const thr = await this.from.throw(e); | ||
return { | ||
done: thr.done, | ||
value: this.mapper(thr.value) | ||
}; | ||
} | ||
[Symbol.asyncIterator]() { | ||
return this; | ||
} | ||
} | ||
function mapAsyncIterableGraphqlResponse(iterable) { | ||
return new AsyncIterableIteratorMapper(iterable, mapGraphqlResponse); | ||
} | ||
exports.mapAsyncIterableGraphqlResponse = mapAsyncIterableGraphqlResponse; |
201317
0.61%4531
0.89%