@envelop/core 1.6.5 → 1.6.6-alpha-1e6c37b.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,31 @@ 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);
141
- }
138
+ function mapAsyncIterator(source, mapper) {
139
+ const iterable = source[Symbol.asyncIterator]();
140
+ const stream = {
141
+ [Symbol.asyncIterator]() {
142
+ return stream;
143
+ },
144
+ async next() {
145
+ const value = await iterable.next();
146
+ if (value.done) {
147
+ return value;
148
+ }
149
+ return { done: false, value: await mapper(value.value) };
150
+ },
151
+ async return() {
152
+ var _a;
153
+ (_a = iterable.return) === null || _a === void 0 ? void 0 : _a.call(iterable);
154
+ return { done: true, value: undefined };
155
+ },
156
+ async throw(error) {
157
+ var _a;
158
+ (_a = iterable.throw) === null || _a === void 0 ? void 0 : _a.call(iterable, error);
159
+ return { done: true, value: undefined };
160
+ },
161
+ };
162
+ return stream;
142
163
  }
143
164
  function getExecuteArgs(args) {
144
165
  return args.length === 1
@@ -189,21 +210,65 @@ function handleStreamOrSingleExecutionResult(payload, fn) {
189
210
  return undefined;
190
211
  }
191
212
  }
192
- async function* finalAsyncIterator(asyncIterable, onFinal) {
193
- try {
194
- yield* asyncIterable;
195
- }
196
- finally {
197
- onFinal();
198
- }
213
+ function finalAsyncIterator(source, onFinal) {
214
+ const iterable = source[Symbol.asyncIterator]();
215
+ let isDone = false;
216
+ const stream = {
217
+ [Symbol.asyncIterator]() {
218
+ return stream;
219
+ },
220
+ async next() {
221
+ const result = await iterable.next();
222
+ if (result.done && isDone === false) {
223
+ isDone = true;
224
+ onFinal();
225
+ }
226
+ return result;
227
+ },
228
+ async return() {
229
+ var _a;
230
+ (_a = iterable.return) === null || _a === void 0 ? void 0 : _a.call(iterable);
231
+ if (isDone === false) {
232
+ isDone = true;
233
+ onFinal();
234
+ }
235
+ return { done: true, value: undefined };
236
+ },
237
+ async throw(error) {
238
+ var _a;
239
+ (_a = iterable.throw) === null || _a === void 0 ? void 0 : _a.call(iterable, error);
240
+ return { done: true, value: undefined };
241
+ },
242
+ };
243
+ return stream;
199
244
  }
200
- async function* errorAsyncIterator(asyncIterable, onError) {
201
- try {
202
- yield* asyncIterable;
203
- }
204
- catch (err) {
205
- onError(err);
206
- }
245
+ function errorAsyncIterator(source, onError) {
246
+ const iterable = source[Symbol.asyncIterator]();
247
+ const stream = {
248
+ [Symbol.asyncIterator]() {
249
+ return stream;
250
+ },
251
+ async next() {
252
+ try {
253
+ return await iterable.next();
254
+ }
255
+ catch (error) {
256
+ onError(error);
257
+ throw error;
258
+ }
259
+ },
260
+ async return() {
261
+ var _a;
262
+ (_a = iterable.return) === null || _a === void 0 ? void 0 : _a.call(iterable);
263
+ return { done: true, value: undefined };
264
+ },
265
+ async throw(error) {
266
+ var _a;
267
+ (_a = iterable.throw) === null || _a === void 0 ? void 0 : _a.call(iterable, error);
268
+ return { done: true, value: undefined };
269
+ },
270
+ };
271
+ return stream;
207
272
  }
208
273
 
209
274
  function createEnvelopOrchestrator(plugins) {
@@ -1010,8 +1075,8 @@ const useExtendContext = (contextFactory) => ({
1010
1075
  },
1011
1076
  });
1012
1077
 
1013
- const makeHandleResult$1 = (formatter) => ({ result, setResult }) => {
1014
- const modified = formatter(result);
1078
+ const makeHandleResult$1 = (formatter) => ({ args, result, setResult, }) => {
1079
+ const modified = formatter(result, args);
1015
1080
  if (modified !== false) {
1016
1081
  setResult(modified);
1017
1082
  }
package/index.mjs CHANGED
@@ -131,10 +131,31 @@ 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);
137
- }
134
+ function mapAsyncIterator(source, mapper) {
135
+ const iterable = source[Symbol.asyncIterator]();
136
+ const stream = {
137
+ [Symbol.asyncIterator]() {
138
+ return stream;
139
+ },
140
+ async next() {
141
+ const value = await iterable.next();
142
+ if (value.done) {
143
+ return value;
144
+ }
145
+ return { done: false, value: await mapper(value.value) };
146
+ },
147
+ async return() {
148
+ var _a;
149
+ (_a = iterable.return) === null || _a === void 0 ? void 0 : _a.call(iterable);
150
+ return { done: true, value: undefined };
151
+ },
152
+ async throw(error) {
153
+ var _a;
154
+ (_a = iterable.throw) === null || _a === void 0 ? void 0 : _a.call(iterable, error);
155
+ return { done: true, value: undefined };
156
+ },
157
+ };
158
+ return stream;
138
159
  }
139
160
  function getExecuteArgs(args) {
140
161
  return args.length === 1
@@ -185,21 +206,65 @@ function handleStreamOrSingleExecutionResult(payload, fn) {
185
206
  return undefined;
186
207
  }
187
208
  }
188
- async function* finalAsyncIterator(asyncIterable, onFinal) {
189
- try {
190
- yield* asyncIterable;
191
- }
192
- finally {
193
- onFinal();
194
- }
209
+ function finalAsyncIterator(source, onFinal) {
210
+ const iterable = source[Symbol.asyncIterator]();
211
+ let isDone = false;
212
+ const stream = {
213
+ [Symbol.asyncIterator]() {
214
+ return stream;
215
+ },
216
+ async next() {
217
+ const result = await iterable.next();
218
+ if (result.done && isDone === false) {
219
+ isDone = true;
220
+ onFinal();
221
+ }
222
+ return result;
223
+ },
224
+ async return() {
225
+ var _a;
226
+ (_a = iterable.return) === null || _a === void 0 ? void 0 : _a.call(iterable);
227
+ if (isDone === false) {
228
+ isDone = true;
229
+ onFinal();
230
+ }
231
+ return { done: true, value: undefined };
232
+ },
233
+ async throw(error) {
234
+ var _a;
235
+ (_a = iterable.throw) === null || _a === void 0 ? void 0 : _a.call(iterable, error);
236
+ return { done: true, value: undefined };
237
+ },
238
+ };
239
+ return stream;
195
240
  }
196
- async function* errorAsyncIterator(asyncIterable, onError) {
197
- try {
198
- yield* asyncIterable;
199
- }
200
- catch (err) {
201
- onError(err);
202
- }
241
+ function errorAsyncIterator(source, onError) {
242
+ const iterable = source[Symbol.asyncIterator]();
243
+ const stream = {
244
+ [Symbol.asyncIterator]() {
245
+ return stream;
246
+ },
247
+ async next() {
248
+ try {
249
+ return await iterable.next();
250
+ }
251
+ catch (error) {
252
+ onError(error);
253
+ throw error;
254
+ }
255
+ },
256
+ async return() {
257
+ var _a;
258
+ (_a = iterable.return) === null || _a === void 0 ? void 0 : _a.call(iterable);
259
+ return { done: true, value: undefined };
260
+ },
261
+ async throw(error) {
262
+ var _a;
263
+ (_a = iterable.throw) === null || _a === void 0 ? void 0 : _a.call(iterable, error);
264
+ return { done: true, value: undefined };
265
+ },
266
+ };
267
+ return stream;
203
268
  }
204
269
 
205
270
  function createEnvelopOrchestrator(plugins) {
@@ -1006,8 +1071,8 @@ const useExtendContext = (contextFactory) => ({
1006
1071
  },
1007
1072
  });
1008
1073
 
1009
- const makeHandleResult$1 = (formatter) => ({ result, setResult }) => {
1010
- const modified = formatter(result);
1074
+ const makeHandleResult$1 = (formatter) => ({ args, result, setResult, }) => {
1075
+ const modified = formatter(result, args);
1011
1076
  if (modified !== false) {
1012
1077
  setResult(modified);
1013
1078
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@envelop/core",
3
- "version": "1.6.5",
3
+ "version": "1.6.6-alpha-1e6c37b.0",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
6
  "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
@@ -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>;