New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Return event IDs when sending events #195
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"inngest": minor | ||
--- | ||
|
||
Return event IDs when sending events using `inngest.send()` and `step.sendEvent()` |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import { z } from "zod"; | ||
import { envKeys } from "../helpers/consts"; | ||
import { devServerAvailable, devServerUrl } from "../helpers/devserver"; | ||
import { | ||
|
@@ -148,7 +149,7 @@ export class Inngest< | |
/** | ||
* Given a response from Inngest, relay the error to the caller. | ||
*/ | ||
async #getResponseError(response: globalThis.Response): Promise<Error> { | ||
#getResponseError(response: globalThis.Response, rawBody: unknown): Error { | ||
let errorMessage = "Unknown error"; | ||
switch (response.status) { | ||
case 401: | ||
|
@@ -164,7 +165,7 @@ export class Inngest< | |
errorMessage = "Event key not found"; | ||
break; | ||
case 406: | ||
errorMessage = `${JSON.stringify(await response.json())}`; | ||
errorMessage = `${JSON.stringify(rawBody)}`; | ||
break; | ||
case 409: | ||
case 412: | ||
|
@@ -224,7 +225,7 @@ export class Inngest< | |
public async send<Event extends keyof Events>( | ||
name: Event, | ||
payload: SingleOrArray<PartialK<Omit<Events[Event], "name" | "v">, "ts">> | ||
): Promise<void>; | ||
): Promise<string[]>; | ||
/** | ||
* Send one or many events to Inngest. Takes an entire payload (including | ||
* name) as each input. | ||
|
@@ -251,7 +252,7 @@ export class Inngest< | |
*/ | ||
public async send<Payload extends SendEventPayload<Events>>( | ||
payload: Payload | ||
): Promise<void>; | ||
): Promise<string[]>; | ||
public async send<Event extends keyof Events>( | ||
nameOrPayload: | ||
| Event | ||
|
@@ -263,7 +264,7 @@ export class Inngest< | |
maybePayload?: SingleOrArray< | ||
PartialK<Omit<Events[Event], "name" | "v">, "ts"> | ||
> | ||
): Promise<void> { | ||
): Promise<string[]> { | ||
if (!this.eventKey) { | ||
throw new Error( | ||
prettyError({ | ||
|
@@ -306,7 +307,7 @@ export class Inngest< | |
* happens, show a warning that this may not be intended, but don't throw. | ||
*/ | ||
if (!payloads.length) { | ||
return console.warn( | ||
console.warn( | ||
prettyError({ | ||
type: "warn", | ||
whatHappened: "`inngest.send()` called with no events", | ||
|
@@ -317,6 +318,8 @@ export class Inngest< | |
stack: true, | ||
}) | ||
); | ||
|
||
return []; | ||
} | ||
|
||
// When sending events, check if the dev server is available. If so, use the | ||
|
@@ -339,11 +342,22 @@ export class Inngest< | |
headers: { ...this.headers }, | ||
}); | ||
|
||
if (response.status >= 200 && response.status < 300) { | ||
return; | ||
} | ||
let rawBody: unknown; | ||
|
||
try { | ||
rawBody = await response.json(); | ||
|
||
throw await this.#getResponseError(response); | ||
const body = await z | ||
.object({ | ||
ids: z.array(z.string()), | ||
status: z.number().min(200).max(299), | ||
}) | ||
.parseAsync(rawBody); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we decide at some point to return more data from the response and the shape of the response changes, would this parsing fail? Or will it only parse the data expected and ignore the rest? |
||
|
||
return body.ids; | ||
} catch (err) { | ||
throw this.#getResponseError(response, rawBody); | ||
} | ||
} | ||
|
||
public createFunction< | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seeing this and the test below, I noticed we may not have a test w/ an array of multiple events, but we do have one w/ an array. Is that something we should cover?