@envelop/core 1.6.4 → 1.6.6-alpha-63e92f8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -135,10 +135,50 @@ function getSubscribeArgs(args) {
135
135
  * Utility function for making a subscribe function that handles polymorphic arguments.
136
136
  */
137
137
  const makeSubscribe = (subscribeFn) => ((...polyArgs) => subscribeFn(getSubscribeArgs(polyArgs)));
138
- async function* mapAsyncIterator(asyncIterable, map) {
139
- for await (const value of asyncIterable) {
140
- yield map(value);
138
+ function mapAsyncIterator(source, mapper) {
139
+ const iterator = source[Symbol.asyncIterator]();
140
+ async function mapResult(result) {
141
+ var _a;
142
+ if (result.done) {
143
+ return result;
144
+ }
145
+ try {
146
+ return { value: await mapper(result.value), done: false };
147
+ }
148
+ catch (error) {
149
+ try {
150
+ await ((_a = iterator.return) === null || _a === void 0 ? void 0 : _a.call(iterator));
151
+ }
152
+ catch (_error) {
153
+ /* ignore error */
154
+ }
155
+ throw error;
156
+ }
141
157
  }
158
+ const stream = {
159
+ [Symbol.asyncIterator]() {
160
+ return stream;
161
+ },
162
+ async next() {
163
+ return await mapResult(await iterator.next());
164
+ },
165
+ async return() {
166
+ var _a;
167
+ const promise = (_a = iterator.return) === null || _a === void 0 ? void 0 : _a.call(iterator);
168
+ return promise ? await mapResult(await promise) : { value: undefined, done: true };
169
+ },
170
+ async throw(error) {
171
+ var _a;
172
+ const promise = (_a = iterator.throw) === null || _a === void 0 ? void 0 : _a.call(iterator);
173
+ if (promise) {
174
+ return await mapResult(await promise);
175
+ }
176
+ // if the source has no throw method we just re-throw error
177
+ // usually throw is not called anyways
178
+ throw error;
179
+ },
180
+ };
181
+ return stream;
142
182
  }
143
183
  function getExecuteArgs(args) {
144
184
  return args.length === 1
@@ -189,21 +229,75 @@ function handleStreamOrSingleExecutionResult(payload, fn) {
189
229
  return undefined;
190
230
  }
191
231
  }
192
- async function* finalAsyncIterator(asyncIterable, onFinal) {
193
- try {
194
- yield* asyncIterable;
195
- }
196
- finally {
197
- onFinal();
198
- }
232
+ function finalAsyncIterator(source, onFinal) {
233
+ const iterator = source[Symbol.asyncIterator]();
234
+ let isDone = false;
235
+ const stream = {
236
+ [Symbol.asyncIterator]() {
237
+ return stream;
238
+ },
239
+ async next() {
240
+ const result = await iterator.next();
241
+ if (result.done && isDone === false) {
242
+ isDone = true;
243
+ onFinal();
244
+ }
245
+ return result;
246
+ },
247
+ async return() {
248
+ var _a;
249
+ const promise = (_a = iterator.return) === null || _a === void 0 ? void 0 : _a.call(iterator);
250
+ if (isDone === false) {
251
+ isDone = true;
252
+ onFinal();
253
+ }
254
+ return promise ? await promise : { done: true, value: undefined };
255
+ },
256
+ async throw(error) {
257
+ var _a;
258
+ const promise = (_a = iterator.throw) === null || _a === void 0 ? void 0 : _a.call(iterator);
259
+ if (promise) {
260
+ return await promise;
261
+ }
262
+ // if the source has no throw method we just re-throw error
263
+ // usually throw is not called anyways
264
+ throw error;
265
+ },
266
+ };
267
+ return stream;
199
268
  }
200
- async function* errorAsyncIterator(asyncIterable, onError) {
201
- try {
202
- yield* asyncIterable;
203
- }
204
- catch (err) {
205
- onError(err);
206
- }
269
+ function errorAsyncIterator(source, onError) {
270
+ const iterator = source[Symbol.asyncIterator]();
271
+ const stream = {
272
+ [Symbol.asyncIterator]() {
273
+ return stream;
274
+ },
275
+ async next() {
276
+ try {
277
+ return await iterator.next();
278
+ }
279
+ catch (error) {
280
+ onError(error);
281
+ return { done: true, value: undefined };
282
+ }
283
+ },
284
+ async return() {
285
+ var _a;
286
+ const promise = (_a = iterator.return) === null || _a === void 0 ? void 0 : _a.call(iterator);
287
+ return promise ? await promise : { done: true, value: undefined };
288
+ },
289
+ async throw(error) {
290
+ var _a;
291
+ const promise = (_a = iterator.throw) === null || _a === void 0 ? void 0 : _a.call(iterator);
292
+ if (promise) {
293
+ return await promise;
294
+ }
295
+ // if the source has no throw method we just re-throw error
296
+ // usually throw is not called anyways
297
+ throw error;
298
+ },
299
+ };
300
+ return stream;
207
301
  }
208
302
 
209
303
  function createEnvelopOrchestrator(plugins) {
@@ -1010,8 +1104,8 @@ const useExtendContext = (contextFactory) => ({
1010
1104
  },
1011
1105
  });
1012
1106
 
1013
- const makeHandleResult$1 = (formatter) => ({ result, setResult }) => {
1014
- const modified = formatter(result);
1107
+ const makeHandleResult$1 = (formatter) => ({ args, result, setResult, }) => {
1108
+ const modified = formatter(result, args);
1015
1109
  if (modified !== false) {
1016
1110
  setResult(modified);
1017
1111
  }
package/index.mjs CHANGED
@@ -131,10 +131,50 @@ function getSubscribeArgs(args) {
131
131
  * Utility function for making a subscribe function that handles polymorphic arguments.
132
132
  */
133
133
  const makeSubscribe = (subscribeFn) => ((...polyArgs) => subscribeFn(getSubscribeArgs(polyArgs)));
134
- async function* mapAsyncIterator(asyncIterable, map) {
135
- for await (const value of asyncIterable) {
136
- yield map(value);
134
+ function mapAsyncIterator(source, mapper) {
135
+ const iterator = source[Symbol.asyncIterator]();
136
+ async function mapResult(result) {
137
+ var _a;
138
+ if (result.done) {
139
+ return result;
140
+ }
141
+ try {
142
+ return { value: await mapper(result.value), done: false };
143
+ }
144
+ catch (error) {
145
+ try {
146
+ await ((_a = iterator.return) === null || _a === void 0 ? void 0 : _a.call(iterator));
147
+ }
148
+ catch (_error) {
149
+ /* ignore error */
150
+ }
151
+ throw error;
152
+ }
137
153
  }
154
+ const stream = {
155
+ [Symbol.asyncIterator]() {
156
+ return stream;
157
+ },
158
+ async next() {
159
+ return await mapResult(await iterator.next());
160
+ },
161
+ async return() {
162
+ var _a;
163
+ const promise = (_a = iterator.return) === null || _a === void 0 ? void 0 : _a.call(iterator);
164
+ return promise ? await mapResult(await promise) : { value: undefined, done: true };
165
+ },
166
+ async throw(error) {
167
+ var _a;
168
+ const promise = (_a = iterator.throw) === null || _a === void 0 ? void 0 : _a.call(iterator);
169
+ if (promise) {
170
+ return await mapResult(await promise);
171
+ }
172
+ // if the source has no throw method we just re-throw error
173
+ // usually throw is not called anyways
174
+ throw error;
175
+ },
176
+ };
177
+ return stream;
138
178
  }
139
179
  function getExecuteArgs(args) {
140
180
  return args.length === 1
@@ -185,21 +225,75 @@ function handleStreamOrSingleExecutionResult(payload, fn) {
185
225
  return undefined;
186
226
  }
187
227
  }
188
- async function* finalAsyncIterator(asyncIterable, onFinal) {
189
- try {
190
- yield* asyncIterable;
191
- }
192
- finally {
193
- onFinal();
194
- }
228
+ function finalAsyncIterator(source, onFinal) {
229
+ const iterator = source[Symbol.asyncIterator]();
230
+ let isDone = false;
231
+ const stream = {
232
+ [Symbol.asyncIterator]() {
233
+ return stream;
234
+ },
235
+ async next() {
236
+ const result = await iterator.next();
237
+ if (result.done && isDone === false) {
238
+ isDone = true;
239
+ onFinal();
240
+ }
241
+ return result;
242
+ },
243
+ async return() {
244
+ var _a;
245
+ const promise = (_a = iterator.return) === null || _a === void 0 ? void 0 : _a.call(iterator);
246
+ if (isDone === false) {
247
+ isDone = true;
248
+ onFinal();
249
+ }
250
+ return promise ? await promise : { done: true, value: undefined };
251
+ },
252
+ async throw(error) {
253
+ var _a;
254
+ const promise = (_a = iterator.throw) === null || _a === void 0 ? void 0 : _a.call(iterator);
255
+ if (promise) {
256
+ return await promise;
257
+ }
258
+ // if the source has no throw method we just re-throw error
259
+ // usually throw is not called anyways
260
+ throw error;
261
+ },
262
+ };
263
+ return stream;
195
264
  }
196
- async function* errorAsyncIterator(asyncIterable, onError) {
197
- try {
198
- yield* asyncIterable;
199
- }
200
- catch (err) {
201
- onError(err);
202
- }
265
+ function errorAsyncIterator(source, onError) {
266
+ const iterator = source[Symbol.asyncIterator]();
267
+ const stream = {
268
+ [Symbol.asyncIterator]() {
269
+ return stream;
270
+ },
271
+ async next() {
272
+ try {
273
+ return await iterator.next();
274
+ }
275
+ catch (error) {
276
+ onError(error);
277
+ return { done: true, value: undefined };
278
+ }
279
+ },
280
+ async return() {
281
+ var _a;
282
+ const promise = (_a = iterator.return) === null || _a === void 0 ? void 0 : _a.call(iterator);
283
+ return promise ? await promise : { done: true, value: undefined };
284
+ },
285
+ async throw(error) {
286
+ var _a;
287
+ const promise = (_a = iterator.throw) === null || _a === void 0 ? void 0 : _a.call(iterator);
288
+ if (promise) {
289
+ return await promise;
290
+ }
291
+ // if the source has no throw method we just re-throw error
292
+ // usually throw is not called anyways
293
+ throw error;
294
+ },
295
+ };
296
+ return stream;
203
297
  }
204
298
 
205
299
  function createEnvelopOrchestrator(plugins) {
@@ -1006,8 +1100,8 @@ const useExtendContext = (contextFactory) => ({
1006
1100
  },
1007
1101
  });
1008
1102
 
1009
- const makeHandleResult$1 = (formatter) => ({ result, setResult }) => {
1010
- const modified = formatter(result);
1103
+ const makeHandleResult$1 = (formatter) => ({ args, result, setResult, }) => {
1104
+ const modified = formatter(result, args);
1011
1105
  if (modified !== false) {
1012
1106
  setResult(modified);
1013
1107
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@envelop/core",
3
- "version": "1.6.4",
3
+ "version": "1.6.6-alpha-63e92f8.0",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
6
  "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
@@ -38,6 +38,7 @@
38
38
  "./*": {
39
39
  "require": "./*.js",
40
40
  "import": "./*.mjs"
41
- }
41
+ },
42
+ "./package.json": "./package.json"
42
43
  }
43
44
  }
@@ -1,5 +1,5 @@
1
1
  import { Plugin } from '@envelop/types';
2
- export declare type ContextFactoryFn<TResult = unknown> = (currentContext: unknown) => TResult | Promise<TResult>;
2
+ export declare type ContextFactoryFn<TResult = unknown, TCurrent = unknown> = (currentContext: TCurrent) => TResult | Promise<TResult>;
3
3
  declare type UnwrapAsync<T> = T extends Promise<infer U> ? U : T;
4
4
  export declare const useExtendContext: <TContextFactory extends (...args: any[]) => any>(contextFactory: TContextFactory) => Plugin<UnwrapAsync<ReturnType<TContextFactory>>>;
5
5
  export {};
@@ -1,4 +1,4 @@
1
- import { Plugin } from '@envelop/types';
1
+ import { Plugin, TypedExecutionArgs } from '@envelop/types';
2
2
  import { ExecutionResult } from 'graphql';
3
- export declare type FormatterFunction = (result: ExecutionResult<any, any>) => false | ExecutionResult<any, any>;
3
+ export declare type FormatterFunction = (result: ExecutionResult<any, any>, args: TypedExecutionArgs<any>) => false | ExecutionResult<any, any>;
4
4
  export declare const usePayloadFormatter: (formatter: FormatterFunction) => Plugin;
package/utils.d.ts CHANGED
@@ -9,7 +9,7 @@ export declare function isIntrospectionOperationString(operation: string | Sourc
9
9
  * Utility function for making a subscribe function that handles polymorphic arguments.
10
10
  */
11
11
  export declare const makeSubscribe: (subscribeFn: (args: SubscriptionArgs) => PromiseOrValue<AsyncIterableIterator<ExecutionResult>>) => SubscribeFunction;
12
- export declare function mapAsyncIterator<TInput, TOutput = TInput>(asyncIterable: AsyncIterable<TInput>, map: (input: TInput) => Promise<TOutput> | TOutput): AsyncIterableIterator<TOutput>;
12
+ export declare function mapAsyncIterator<T, O>(source: AsyncIterable<T>, mapper: (input: T) => Promise<O> | O): AsyncGenerator<O>;
13
13
  /**
14
14
  * Utility function for making a execute function that handles polymorphic arguments.
15
15
  */
@@ -29,5 +29,5 @@ export declare function isAsyncIterable<TType>(maybeAsyncIterable: unknown): may
29
29
  * @returns a subscription for streamed results, or undefined in case of an non-async
30
30
  */
31
31
  export declare function handleStreamOrSingleExecutionResult<ContextType = DefaultContext>(payload: OnExecuteDoneEventPayload<ContextType>, fn: OnExecuteDoneHookResultOnNextHook<ContextType>): void | OnExecuteDoneHookResult<ContextType>;
32
- export declare function finalAsyncIterator<TInput>(asyncIterable: AsyncIterable<TInput>, onFinal: () => void): AsyncIterableIterator<TInput>;
33
- export declare function errorAsyncIterator<TInput>(asyncIterable: AsyncIterable<TInput>, onError: (err: unknown) => void): AsyncIterableIterator<TInput>;
32
+ export declare function finalAsyncIterator<TInput>(source: AsyncIterable<TInput>, onFinal: () => void): AsyncGenerator<TInput>;
33
+ export declare function errorAsyncIterator<TInput>(source: AsyncIterable<TInput>, onError: (err: unknown) => void): AsyncGenerator<TInput>;