inngest 1.8.5 → 2.0.0-next.1

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.
Files changed (56) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/components/EventSchemas.d.ts +154 -0
  3. package/components/EventSchemas.d.ts.map +1 -0
  4. package/components/EventSchemas.js +112 -0
  5. package/components/EventSchemas.js.map +1 -0
  6. package/components/Inngest.d.ts +70 -33
  7. package/components/Inngest.d.ts.map +1 -1
  8. package/components/Inngest.js +121 -25
  9. package/components/Inngest.js.map +1 -1
  10. package/components/InngestCommHandler.d.ts +2 -2
  11. package/components/InngestCommHandler.d.ts.map +1 -1
  12. package/components/InngestCommHandler.js +7 -1
  13. package/components/InngestCommHandler.js.map +1 -1
  14. package/components/InngestFunction.d.ts +72 -5
  15. package/components/InngestFunction.d.ts.map +1 -1
  16. package/components/InngestFunction.js +302 -225
  17. package/components/InngestFunction.js.map +1 -1
  18. package/components/InngestMiddleware.d.ts +295 -0
  19. package/components/InngestMiddleware.d.ts.map +1 -0
  20. package/components/InngestMiddleware.js +96 -0
  21. package/components/InngestMiddleware.js.map +1 -0
  22. package/components/InngestRun.d.ts +17 -0
  23. package/components/InngestRun.d.ts.map +1 -0
  24. package/components/InngestRun.js +25 -0
  25. package/components/InngestRun.js.map +1 -0
  26. package/components/InngestStepTools.d.ts +7 -71
  27. package/components/InngestStepTools.d.ts.map +1 -1
  28. package/components/InngestStepTools.js +6 -41
  29. package/components/InngestStepTools.js.map +1 -1
  30. package/helpers/functions.d.ts +15 -0
  31. package/helpers/functions.d.ts.map +1 -1
  32. package/helpers/functions.js +36 -1
  33. package/helpers/functions.js.map +1 -1
  34. package/helpers/types.d.ts +16 -0
  35. package/helpers/types.d.ts.map +1 -1
  36. package/index.d.ts +6 -1
  37. package/index.d.ts.map +1 -1
  38. package/index.js +5 -1
  39. package/index.js.map +1 -1
  40. package/landing.d.ts +1 -1
  41. package/landing.d.ts.map +1 -1
  42. package/landing.js +1 -1
  43. package/landing.js.map +1 -1
  44. package/middleware/logger.d.ts +48 -0
  45. package/middleware/logger.d.ts.map +1 -0
  46. package/middleware/logger.js +90 -0
  47. package/middleware/logger.js.map +1 -0
  48. package/package.json +1 -1
  49. package/types.d.ts +84 -17
  50. package/types.d.ts.map +1 -1
  51. package/types.js +8 -1
  52. package/types.js.map +1 -1
  53. package/version.d.ts +1 -1
  54. package/version.d.ts.map +1 -1
  55. package/version.js +1 -1
  56. package/version.js.map +1 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # inngest
2
2
 
3
+ ## 1.9.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 48d94a2: Allow user provided logger to be used within functions (experimental)
8
+
3
9
  ## 1.8.5
4
10
 
5
11
  ### Patch Changes
@@ -0,0 +1,154 @@
1
+ import { type Simplify } from "type-fest";
2
+ import { type z } from "zod";
3
+ import { type IsStringLiteral } from "../helpers/types";
4
+ import { type EventPayload } from "../types";
5
+ export type StandardEventSchema = {
6
+ data: Record<string, any>;
7
+ user?: Record<string, any>;
8
+ };
9
+ /**
10
+ * A helper type that declares a standardised custom part of the event schema.
11
+ *
12
+ * @public
13
+ */
14
+ export type StandardEventSchemas = Record<string, StandardEventSchema>;
15
+ /**
16
+ * A helper type that declares a standardised custom part of the event schema,
17
+ * defined using Zod.
18
+ *
19
+ * @public
20
+ */
21
+ export type ZodEventSchemas = Record<string, {
22
+ data: z.AnyZodObject | z.ZodAny;
23
+ user?: z.AnyZodObject | z.ZodAny;
24
+ }>;
25
+ /**
26
+ * A helper type to convert input schemas into the format expected by the
27
+ * `EventSchemas` class, which ensures that each event contains all pieces
28
+ * of information required.
29
+ *
30
+ * It purposefully uses slightly more complex (read: verbose) mapped types to
31
+ * flatten the output.
32
+ *
33
+ * @public
34
+ */
35
+ export type StandardEventSchemaToPayload<T> = Simplify<{
36
+ [K in keyof T & string]: {
37
+ [K2 in keyof (Omit<EventPayload, keyof T[K]> & T[K] & {
38
+ name: K;
39
+ })]: (Omit<EventPayload, keyof T[K]> & T[K] & {
40
+ name: K;
41
+ })[K2];
42
+ };
43
+ }>;
44
+ /**
45
+ * A helper type to combine two event schemas together, ensuring the result is
46
+ * as flat as possible and that we don't accidentally overwrite existing schemas
47
+ * with a generic `string` key.
48
+ *
49
+ * @public
50
+ */
51
+ export type Combine<TCurr extends Record<string, EventPayload>, TInc extends StandardEventSchemas> = IsStringLiteral<keyof TCurr & string> extends true ? Simplify<Omit<TCurr, keyof StandardEventSchemaToPayload<TInc>> & StandardEventSchemaToPayload<TInc>> : StandardEventSchemaToPayload<TInc>;
52
+ /**
53
+ * Provide an `EventSchemas` class to type events, providing type safety when
54
+ * sending events and running functions via Inngest.
55
+ *
56
+ * You can provide generated Inngest types, custom types, types using Zod, or
57
+ * a combination of the above. See {@link EventSchemas} for more information.
58
+ *
59
+ * @example
60
+ *
61
+ * ```ts
62
+ * export const inngest = new Inngest({
63
+ * name: "My App",
64
+ * schemas: new EventSchemas().fromZod({
65
+ * "app/user.created": {
66
+ * data: z.object({
67
+ * id: z.string(),
68
+ * name: z.string(),
69
+ * }),
70
+ * },
71
+ * }),
72
+ * });
73
+ * ```
74
+ *
75
+ * @public
76
+ */
77
+ export declare class EventSchemas<S extends Record<string, EventPayload>> {
78
+ /**
79
+ * Use generated Inngest types to type events.
80
+ */
81
+ fromGenerated<T extends StandardEventSchemas>(): EventSchemas<Combine<S, T>>;
82
+ /**
83
+ * Use a `Record<>` type to type events.
84
+ *
85
+ * @example
86
+ *
87
+ * ```ts
88
+ * export const inngest = new Inngest({
89
+ * name: "My App",
90
+ * schemas: new EventSchemas().fromRecord<{
91
+ * "app/user.created": {
92
+ * data: {
93
+ * id: string;
94
+ * name: string;
95
+ * };
96
+ * };
97
+ * }>(),
98
+ * });
99
+ * ```
100
+ */
101
+ fromRecord<T extends StandardEventSchemas>(): EventSchemas<Combine<S, T>>;
102
+ /**
103
+ * Use a union type to type events.
104
+ *
105
+ * @example
106
+ *
107
+ * ```ts
108
+ * type AccountCreated = {
109
+ * name: "app/account.created";
110
+ * data: { org: string };
111
+ * user: { id: string };
112
+ * };
113
+ *
114
+ * type AccountDeleted = {
115
+ * name: "app/account.deleted";
116
+ * data: { org: string };
117
+ * user: { id: string };
118
+ * };
119
+ *
120
+ * type Events = AccountCreated | AccountDeleted;
121
+ *
122
+ * export const inngest = new Inngest({
123
+ * name: "My App",
124
+ * schemas: new EventSchemas().fromUnion<Events>(),
125
+ * });
126
+ * ```
127
+ */
128
+ fromUnion<T extends {
129
+ name: string;
130
+ } & StandardEventSchema>(): EventSchemas<Combine<S, { [K in T["name"]]: Extract<T, {
131
+ name: K;
132
+ }>; }>>;
133
+ /**
134
+ * Use Zod to type events.
135
+ *
136
+ * @example
137
+ *
138
+ * ```ts
139
+ * export const inngest = new Inngest({
140
+ * name: "My App",
141
+ * schemas: new EventSchemas().fromZod({
142
+ * "app/user.created": {
143
+ * data: z.object({
144
+ * id: z.string(),
145
+ * name: z.string(),
146
+ * }),
147
+ * },
148
+ * }),
149
+ * });
150
+ * ```
151
+ */
152
+ fromZod<T extends ZodEventSchemas>(schemas: T): EventSchemas<Combine<S, { [EventName in keyof T & string]: { [Key in keyof T[EventName] & string]: T[EventName][Key] extends z.ZodTypeAny ? z.TypeOf<T[EventName][Key]> : T[EventName][Key]; }; }>>;
153
+ }
154
+ //# sourceMappingURL=EventSchemas.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EventSchemas.d.ts","sourceRoot":"","sources":["../../src/components/EventSchemas.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,KAAK,CAAC,EAAE,MAAM,KAAK,CAAC;AAC7B,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,kBAAkB,CAAC;AACxD,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,UAAU,CAAC;AAE7C,MAAM,MAAM,mBAAmB,GAAG;IAEhC,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ACrC,GACD,4BAA4B,CAAC,IAAI,CAAC,CAAC;AAEvC;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,qBAAa,YAAY,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC;IAC9D;;OAEG;IACI,aAAa,CAAC,CAAC,SAAS,oBAAoB;IAInD;;;;;;;;;;;;;;;;;;OAkBG;IACI,UAAU,CAAC,CAAC,SAAS,oBAAoB;IAIhD;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACI,SAAS,CAAC,CAAC,SAAS;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,GAAG,mBAAmB;;;IAWjE;;;;;;;;;;;;;;;;;;OAkBG;IAEI,OAAO,CAAC,CAAC,SAAS,eAAe,EAAE,OAAO,EAAE,CAAC;CAerD"}
@@ -0,0 +1,112 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EventSchemas = void 0;
4
+ /**
5
+ * Provide an `EventSchemas` class to type events, providing type safety when
6
+ * sending events and running functions via Inngest.
7
+ *
8
+ * You can provide generated Inngest types, custom types, types using Zod, or
9
+ * a combination of the above. See {@link EventSchemas} for more information.
10
+ *
11
+ * @example
12
+ *
13
+ * ```ts
14
+ * export const inngest = new Inngest({
15
+ * name: "My App",
16
+ * schemas: new EventSchemas().fromZod({
17
+ * "app/user.created": {
18
+ * data: z.object({
19
+ * id: z.string(),
20
+ * name: z.string(),
21
+ * }),
22
+ * },
23
+ * }),
24
+ * });
25
+ * ```
26
+ *
27
+ * @public
28
+ */
29
+ class EventSchemas {
30
+ /**
31
+ * Use generated Inngest types to type events.
32
+ */
33
+ fromGenerated() {
34
+ return new EventSchemas();
35
+ }
36
+ /**
37
+ * Use a `Record<>` type to type events.
38
+ *
39
+ * @example
40
+ *
41
+ * ```ts
42
+ * export const inngest = new Inngest({
43
+ * name: "My App",
44
+ * schemas: new EventSchemas().fromRecord<{
45
+ * "app/user.created": {
46
+ * data: {
47
+ * id: string;
48
+ * name: string;
49
+ * };
50
+ * };
51
+ * }>(),
52
+ * });
53
+ * ```
54
+ */
55
+ fromRecord() {
56
+ return new EventSchemas();
57
+ }
58
+ /**
59
+ * Use a union type to type events.
60
+ *
61
+ * @example
62
+ *
63
+ * ```ts
64
+ * type AccountCreated = {
65
+ * name: "app/account.created";
66
+ * data: { org: string };
67
+ * user: { id: string };
68
+ * };
69
+ *
70
+ * type AccountDeleted = {
71
+ * name: "app/account.deleted";
72
+ * data: { org: string };
73
+ * user: { id: string };
74
+ * };
75
+ *
76
+ * type Events = AccountCreated | AccountDeleted;
77
+ *
78
+ * export const inngest = new Inngest({
79
+ * name: "My App",
80
+ * schemas: new EventSchemas().fromUnion<Events>(),
81
+ * });
82
+ * ```
83
+ */
84
+ fromUnion() {
85
+ return new EventSchemas();
86
+ }
87
+ /**
88
+ * Use Zod to type events.
89
+ *
90
+ * @example
91
+ *
92
+ * ```ts
93
+ * export const inngest = new Inngest({
94
+ * name: "My App",
95
+ * schemas: new EventSchemas().fromZod({
96
+ * "app/user.created": {
97
+ * data: z.object({
98
+ * id: z.string(),
99
+ * name: z.string(),
100
+ * }),
101
+ * },
102
+ * }),
103
+ * });
104
+ * ```
105
+ */
106
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
107
+ fromZod(schemas) {
108
+ return new EventSchemas();
109
+ }
110
+ }
111
+ exports.EventSchemas = EventSchemas;
112
+ //# sourceMappingURL=EventSchemas.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EventSchemas.js","sourceRoot":"","sources":["../../src/components/EventSchemas.ts"],"names":[],"mappings":";;;AAsEA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAa,YAAY;IACvB;;OAEG;IACI,aAAa;QAClB,OAAO,IAAI,YAAY,EAAiB,CAAC;IAC3C,CAAC;IAED;;;;;;;;;;;;;;;;;;OAkBG;IACI,UAAU;QACf,OAAO,IAAI,YAAY,EAAiB,CAAC;IAC3C,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACI,SAAS;QACd,OAAO,IAAI,YAAY,EAOpB,CAAC;IACN,CAAC;IAED;;;;;;;;;;;;;;;;;;OAkBG;IACH,6DAA6D;IACtD,OAAO,CAA4B,OAAU;QAClD,OAAO,IAAI,YAAY,EAYpB,CAAC;IACN,CAAC;CACF;AAvGD,oCAuGC"}
@@ -1,6 +1,16 @@
1
- import { type PartialK, type SendEventPayload, type SingleOrArray } from "../helpers/types";
2
- import { type ClientOptions, type EventNameFromTrigger, type EventPayload, type FailureEventArgs, type FunctionOptions, type Handler, type ShimmedFns, type TriggerOptions } from "../types";
1
+ import { type SendEventPayload } from "../helpers/types";
2
+ import { type Logger } from "../middleware/logger";
3
+ import { type ClientOptions, type EventNameFromTrigger, type EventPayload, type FailureEventArgs, type FunctionOptions, type FunctionTrigger, type Handler, type MiddlewareStack, type ShimmedFns, type TriggerOptions } from "../types";
4
+ import { type EventSchemas } from "./EventSchemas";
3
5
  import { InngestFunction } from "./InngestFunction";
6
+ import { InngestMiddleware, type MiddlewareStackRunInputMutation } from "./InngestMiddleware";
7
+ /**
8
+ * Given a set of client options for Inngest, return the event types that can
9
+ * be sent or received.
10
+ *
11
+ * @public
12
+ */
13
+ export type EventsFromOpts<TOpts extends ClientOptions> = TOpts["schemas"] extends EventSchemas<infer U> ? U : Record<string, EventPayload>;
4
14
  /**
5
15
  * A client used to interact with the Inngest API by sending or reacting to
6
16
  * events.
@@ -26,7 +36,7 @@ import { InngestFunction } from "./InngestFunction";
26
36
  *
27
37
  * @public
28
38
  */
29
- export declare class Inngest<Events extends Record<string, EventPayload> = Record<string, EventPayload>> {
39
+ export declare class Inngest<TOpts extends ClientOptions = ClientOptions> {
30
40
  #private;
31
41
  /**
32
42
  * The name of this instance, most commonly the name of the application it
@@ -47,6 +57,12 @@ export declare class Inngest<Events extends Record<string, EventPayload> = Recor
47
57
  private inngestApiUrl;
48
58
  private readonly headers;
49
59
  private readonly fetch;
60
+ private readonly logger;
61
+ /**
62
+ * A promise that resolves when the middleware stack has been initialized and
63
+ * the client is ready to be used.
64
+ */
65
+ private readonly middleware;
50
66
  /**
51
67
  * A client used to interact with the Inngest API by sending or reacting to
52
68
  * events.
@@ -70,7 +86,12 @@ export declare class Inngest<Events extends Record<string, EventPayload> = Recor
70
86
  * >({ name: "My App" });
71
87
  * ```
72
88
  */
73
- constructor({ name, eventKey, inngestBaseUrl, fetch, env, }: ClientOptions);
89
+ constructor({ name, eventKey, inngestBaseUrl, fetch, env, logger, middleware, }: TOpts);
90
+ /**
91
+ * Initialize all passed middleware, running the `register` function on each
92
+ * in sequence and returning the requested hook registrations.
93
+ */
94
+ private initializeMiddleware;
74
95
  /**
75
96
  * Set the event key for this instance of Inngest. This is useful if for some
76
97
  * reason the key is not available at time of instantiation or present in the
@@ -83,31 +104,6 @@ export declare class Inngest<Events extends Record<string, EventPayload> = Recor
83
104
  * in the `INNGEST_EVENT_KEY` environment variable.
84
105
  */
85
106
  eventKey: string): void;
86
- /**
87
- * Send one or many events to Inngest. Takes a known event from this Inngest
88
- * instance based on the given `name`.
89
- *
90
- * ```ts
91
- * await inngest.send("app/user.created", { data: { id: 123 } });
92
- * ```
93
- *
94
- * Returns a promise that will resolve if the event(s) were sent successfully,
95
- * else throws with an error explaining what went wrong.
96
- *
97
- * If you wish to send an event with custom types (i.e. one that hasn't been
98
- * generated), make sure to add it when creating your Inngest instance, like
99
- * so:
100
- *
101
- * ```ts
102
- * const inngest = new Inngest<Events & {
103
- * "my/event": {
104
- * name: "my/event";
105
- * data: { bar: string; };
106
- * }
107
- * }>("My App", "API_KEY");
108
- * ```
109
- */
110
- send<Event extends keyof Events>(name: Event, payload: SingleOrArray<PartialK<Omit<Events[Event], "name" | "v">, "ts">>): Promise<void>;
111
107
  /**
112
108
  * Send one or many events to Inngest. Takes an entire payload (including
113
109
  * name) as each input.
@@ -132,8 +128,8 @@ export declare class Inngest<Events extends Record<string, EventPayload> = Recor
132
128
  * }>("My App", "API_KEY");
133
129
  * ```
134
130
  */
135
- send<Payload extends SendEventPayload<Events>>(payload: Payload): Promise<void>;
136
- createFunction<TFns extends Record<string, unknown>, TTrigger extends TriggerOptions<keyof Events & string>, TShimmedFns extends Record<string, (...args: any[]) => any> = ShimmedFns<TFns>, TTriggerName extends keyof Events & string = EventNameFromTrigger<Events, TTrigger>>(nameOrOpts: string | (Omit<FunctionOptions<Events, TTriggerName>, "fns" | "onFailure"> & {
131
+ send<Payload extends SendEventPayload<EventsFromOpts<TOpts>>>(payload: Payload): Promise<void>;
132
+ createFunction<TFns extends Record<string, unknown>, TMiddleware extends MiddlewareStack, TTrigger extends TriggerOptions<keyof EventsFromOpts<TOpts> & string>, TShimmedFns extends Record<string, (...args: any[]) => any> = ShimmedFns<TFns>, TTriggerName extends keyof EventsFromOpts<TOpts> & string = EventNameFromTrigger<EventsFromOpts<TOpts>, TTrigger>>(nameOrOpts: string | (Omit<FunctionOptions<EventsFromOpts<TOpts>, TTriggerName>, "fns" | "onFailure" | "middleware"> & {
137
133
  /**
138
134
  * Pass in an object of functions that will be wrapped in Inngest
139
135
  * tooling and passes to your handler. This wrapping ensures that each
@@ -175,7 +171,48 @@ export declare class Inngest<Events extends Record<string, EventPayload> = Recor
175
171
  * after a failure and supports all the same functionality as a
176
172
  * regular handler.
177
173
  */
178
- onFailure?: Handler<Events, TTriggerName, TShimmedFns, FailureEventArgs<Events[TTriggerName]>>;
179
- }), trigger: TTrigger, handler: Handler<Events, TTriggerName, TShimmedFns>): InngestFunction;
174
+ onFailure?: Handler<TOpts, EventsFromOpts<TOpts>, TTriggerName, TShimmedFns, FailureEventArgs<EventsFromOpts<TOpts>[TTriggerName]>>;
175
+ /**
176
+ * TODO
177
+ */
178
+ middleware?: TMiddleware;
179
+ }), trigger: TTrigger, handler: Handler<TOpts, EventsFromOpts<TOpts>, TTriggerName, TShimmedFns, MiddlewareStackRunInputMutation<{}, typeof builtInMiddleware> & MiddlewareStackRunInputMutation<{}, NonNullable<TOpts["middleware"]>> & MiddlewareStackRunInputMutation<{}, TMiddleware>>): InngestFunction<TOpts, EventsFromOpts<TOpts>, FunctionTrigger<keyof EventsFromOpts<TOpts> & string>, FunctionOptions<EventsFromOpts<TOpts>, keyof EventsFromOpts<TOpts> & string>>;
180
180
  }
181
+ /**
182
+ * Default middleware that is included in every client, placed after the user's
183
+ * middleware on the client but before function-level middleware.
184
+ *
185
+ * It is defined here to ensure that comments are included in the generated TS
186
+ * definitions. Without this, we infer the stack of built-in middleware without
187
+ * comments, losing a lot of value.
188
+ *
189
+ * If this is moved, please ensure that using this package in another project
190
+ * can correctly access comments on mutated input and output.
191
+ */
192
+ declare const builtInMiddleware: [InngestMiddleware<{
193
+ name: string;
194
+ register({ client }: {
195
+ client: Inngest<any>;
196
+ fn?: InngestFunction<any, any, any, any> | undefined;
197
+ }): {
198
+ run(): {
199
+ input(): {
200
+ ctx: {
201
+ /**
202
+ * The passed in logger from the user.
203
+ * Defaults to a console logger if not provided.
204
+ */
205
+ logger: Logger;
206
+ };
207
+ };
208
+ beforeExecution(): void;
209
+ output({ result: { error } }: {
210
+ result: Readonly<Pick<import("../types").OutgoingOp, "data" | "error">>;
211
+ step?: Readonly<Omit<import("../types").OutgoingOp, "id">> | undefined;
212
+ }): void;
213
+ beforeResponse(): Promise<void>;
214
+ };
215
+ };
216
+ }>];
217
+ export {};
181
218
  //# sourceMappingURL=Inngest.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Inngest.d.ts","sourceRoot":"","sources":["../../src/components/Inngest.ts"],"names":[],"mappings":"AAUA,OAAO,EACL,KAAK,QAAQ,EACb,KAAK,gBAAgB,EACrB,KAAK,aAAa,EAEnB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EACL,KAAK,aAAa,EAClB,KAAK,oBAAoB,EACzB,KAAK,YAAY,EACjB,KAAK,gBAAgB,EACrB,KAAK,eAAe,EACpB,KAAK,OAAO,EACZ,KAAK,UAAU,EACf,KAAK,cAAc,EACpB,MAAM,UAAU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,CAAC,EACrD,eAAe,CAAC,cAAc,CAAC,KAAK,CAAC,EAAE,MAAM,cAAc,CAAC,KAAK,CAAC,GAAG,MAAM,CAAC,CAC7E;CAgBF;AAED;;;;;;;;;;GAUG;AACH,QAAA,MAAM,iBAAiB;;;;;;;;;oBAYL;;;uBAGG;;;;;;;;;;;;GAqBnB,CAAC"}
@@ -11,7 +11,9 @@ const consts_1 = require("../helpers/consts");
11
11
  const devserver_1 = require("../helpers/devserver");
12
12
  const env_1 = require("../helpers/env");
13
13
  const errors_1 = require("../helpers/errors");
14
+ const logger_1 = require("../middleware/logger");
14
15
  const InngestFunction_1 = require("./InngestFunction");
16
+ const InngestMiddleware_1 = require("./InngestMiddleware");
15
17
  /**
16
18
  * A client used to interact with the Inngest API by sending or reacting to
17
19
  * events.
@@ -61,7 +63,7 @@ class Inngest {
61
63
  * >({ name: "My App" });
62
64
  * ```
63
65
  */
64
- constructor({ name, eventKey, inngestBaseUrl = "https://inn.gs/", fetch, env, }) {
66
+ constructor({ name, eventKey, inngestBaseUrl = "https://inn.gs/", fetch, env, logger = new logger_1.DefaultLogger(), middleware, }) {
65
67
  _Inngest_instances.add(this);
66
68
  /**
67
69
  * Inngest event key, used to send events to Inngest Cloud.
@@ -92,6 +94,30 @@ class Inngest {
92
94
  inngestEnv: env,
93
95
  });
94
96
  this.fetch = (0, env_1.getFetch)(fetch);
97
+ this.logger = logger;
98
+ this.middleware = this.initializeMiddleware([
99
+ ...builtInMiddleware,
100
+ ...(middleware || []),
101
+ ]);
102
+ }
103
+ /**
104
+ * Initialize all passed middleware, running the `register` function on each
105
+ * in sequence and returning the requested hook registrations.
106
+ */
107
+ async initializeMiddleware(middleware = [], opts) {
108
+ var _a;
109
+ /**
110
+ * Wait for the prefix stack to run first; do not trigger ours before this
111
+ * is complete.
112
+ */
113
+ const prefix = await ((_a = opts === null || opts === void 0 ? void 0 : opts.prefixStack) !== null && _a !== void 0 ? _a : []);
114
+ const stack = middleware.reduce(async (acc, m) => {
115
+ // Be explicit about waiting for the previous middleware to finish
116
+ const prev = await acc;
117
+ const next = await m.register(Object.assign({ client: this }, opts === null || opts === void 0 ? void 0 : opts.registerInput));
118
+ return [...prev, next];
119
+ }, Promise.resolve([]));
120
+ return [...prefix, ...(await stack)];
95
121
  }
96
122
  /**
97
123
  * Set the event key for this instance of Inngest. This is useful if for some
@@ -108,7 +134,40 @@ class Inngest {
108
134
  this.eventKey = eventKey;
109
135
  this.inngestApiUrl = new URL(`e/${this.eventKey}`, this.inngestBaseUrl);
110
136
  }
111
- async send(nameOrPayload, maybePayload) {
137
+ /**
138
+ * Send one or many events to Inngest. Takes an entire payload (including
139
+ * name) as each input.
140
+ *
141
+ * ```ts
142
+ * await inngest.send({ name: "app/user.created", data: { id: 123 } });
143
+ * ```
144
+ *
145
+ * Returns a promise that will resolve if the event(s) were sent successfully,
146
+ * else throws with an error explaining what went wrong.
147
+ *
148
+ * If you wish to send an event with custom types (i.e. one that hasn't been
149
+ * generated), make sure to add it when creating your Inngest instance, like
150
+ * so:
151
+ *
152
+ * ```ts
153
+ * const inngest = new Inngest<Events & {
154
+ * "my/event": {
155
+ * name: "my/event";
156
+ * data: { bar: string; };
157
+ * }
158
+ * }>("My App", "API_KEY");
159
+ * ```
160
+ */
161
+ async send(payload) {
162
+ var _a, _b;
163
+ const hooks = await (0, InngestMiddleware_1.getHookStack)(this.middleware, "sendEvent", undefined, {
164
+ input: (prev, output) => {
165
+ return Object.assign(Object.assign({}, prev), output);
166
+ },
167
+ output: (prev, _output) => {
168
+ return prev;
169
+ },
170
+ });
112
171
  if (!this.eventKey) {
113
172
  throw new Error((0, errors_1.prettyError)({
114
173
  whatHappened: "Failed to send event",
@@ -117,26 +176,14 @@ class Inngest {
117
176
  toFixNow: errors_1.fixEventKeyMissingSteps,
118
177
  }));
119
178
  }
120
- let payloads;
121
- if (typeof nameOrPayload === "string") {
122
- /**
123
- * Add our payloads and ensure they all have a name.
124
- */
125
- payloads = (Array.isArray(maybePayload)
126
- ? maybePayload
127
- : maybePayload
128
- ? [maybePayload]
129
- : []).map((payload) => (Object.assign(Object.assign({}, payload), { name: nameOrPayload })));
130
- }
131
- else {
132
- /**
133
- * Grab our payloads straight from the args.
134
- */
135
- payloads = (Array.isArray(nameOrPayload)
136
- ? nameOrPayload
137
- : nameOrPayload
138
- ? [nameOrPayload]
139
- : []);
179
+ let payloads = Array.isArray(payload)
180
+ ? payload
181
+ : payload
182
+ ? [payload]
183
+ : [];
184
+ const inputChanges = await ((_a = hooks.input) === null || _a === void 0 ? void 0 : _a.call(hooks, { payloads: [...payloads] }));
185
+ if (inputChanges === null || inputChanges === void 0 ? void 0 : inputChanges.payloads) {
186
+ payloads = [...inputChanges.payloads];
140
187
  }
141
188
  /**
142
189
  * It can be valid for a user to send an empty list of events; if this
@@ -169,13 +216,15 @@ class Inngest {
169
216
  headers: Object.assign({}, this.headers),
170
217
  });
171
218
  if (response.status >= 200 && response.status < 300) {
172
- return;
219
+ return void (await ((_b = hooks.output) === null || _b === void 0 ? void 0 : _b.call(hooks)));
173
220
  }
174
221
  throw await __classPrivateFieldGet(this, _Inngest_instances, "m", _Inngest_getResponseError).call(this, response);
175
222
  }
176
223
  createFunction(nameOrOpts, trigger, handler) {
177
- const opts = (typeof nameOrOpts === "string" ? { name: nameOrOpts } : nameOrOpts);
178
- return new InngestFunction_1.InngestFunction(this, opts, typeof trigger === "string" ? { event: trigger } : trigger, handler);
224
+ const sanitizedOpts = (typeof nameOrOpts === "string" ? { name: nameOrOpts } : nameOrOpts);
225
+ return new InngestFunction_1.InngestFunction(this, sanitizedOpts, typeof trigger === "string" ? { event: trigger } : trigger,
226
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
227
+ handler);
179
228
  }
180
229
  }
181
230
  exports.Inngest = Inngest;
@@ -214,4 +263,51 @@ async function _Inngest_getResponseError(response) {
214
263
  }
215
264
  return new Error(`Inngest API Error: ${response.status} ${errorMessage}`);
216
265
  };
266
+ /**
267
+ * Default middleware that is included in every client, placed after the user's
268
+ * middleware on the client but before function-level middleware.
269
+ *
270
+ * It is defined here to ensure that comments are included in the generated TS
271
+ * definitions. Without this, we infer the stack of built-in middleware without
272
+ * comments, losing a lot of value.
273
+ *
274
+ * If this is moved, please ensure that using this package in another project
275
+ * can correctly access comments on mutated input and output.
276
+ */
277
+ const builtInMiddleware = ((m) => m)([
278
+ new InngestMiddleware_1.InngestMiddleware({
279
+ name: "Inngest: Logger",
280
+ register({ client }) {
281
+ return {
282
+ run() {
283
+ const logger = new logger_1.ProxyLogger(client["logger"]);
284
+ return {
285
+ input() {
286
+ return {
287
+ ctx: {
288
+ /**
289
+ * The passed in logger from the user.
290
+ * Defaults to a console logger if not provided.
291
+ */
292
+ logger: logger,
293
+ },
294
+ };
295
+ },
296
+ beforeExecution() {
297
+ logger.enable();
298
+ },
299
+ output({ result: { error } }) {
300
+ if (error) {
301
+ logger.error(error);
302
+ }
303
+ },
304
+ async beforeResponse() {
305
+ await logger.flush();
306
+ },
307
+ };
308
+ },
309
+ };
310
+ },
311
+ }),
312
+ ]);
217
313
  //# sourceMappingURL=Inngest.js.map