Skip to content

Commit

Permalink
Export [component].Any types as helpers (#459)
Browse files Browse the repository at this point in the history
## Summary
<!-- Succinctly describe your change, providing context, what you've
changed, and why. -->

Adds `[component].Any` types such as `Inngest.Any` and
`InngestFunction.Any` to allow easier typing for collections or
factories of pieces of the SDK.

- `Inngest.Any`
- `InngestFunction.Any`
- `Context.Any`
- `Handler.Any`

```ts
import { type InngestFunction } from "inngest";

const functionsToServe: InngestFunction.Any[] = [];
```

## Checklist
<!-- Tick these items off as you progress. -->
<!-- If an item isn't applicable, ideally please strikeout the item by
wrapping it in "~~"" and suffix it with "N/A My reason for skipping
this." -->
<!-- e.g. "- [ ] ~~Added tests~~ N/A Only touches docs" -->

- [x] Added a [docs PR](https://github.com/inngest/website) that
references this PR
- [ ] ~~Added unit/integration tests~~ N/A
- [x] Added changesets if applicable

## Related
<!-- A space for any related links, issues, or PRs. -->
<!-- Linear issues are autolinked. -->
<!-- e.g. - INN-123 -->
<!-- GitHub issues/PRs can be linked using shorthand. -->
<!-- e.g. "- inngest/inngest#123" -->
<!-- Feel free to remove this section if there are no applicable related
links.-->
- inngest/website#627
  • Loading branch information
jpwilliams committed Jan 12, 2024
1 parent a452cf1 commit eec41d2
Show file tree
Hide file tree
Showing 13 changed files with 172 additions and 95 deletions.
5 changes: 5 additions & 0 deletions .changeset/weak-olives-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"inngest": minor
---

Add new `Inngest.Any` and `InngestFunction.Any` type helpers
53 changes: 35 additions & 18 deletions packages/inngest/etc/inngest.api.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 35 additions & 8 deletions packages/inngest/src/components/Inngest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { type ExclusiveKeys, type SendEventPayload } from "../helpers/types";
import { DefaultLogger, ProxyLogger, type Logger } from "../middleware/logger";
import {
sendEventResponseSchema,
type AnyHandler,
type ClientOptions,
type EventNameFromTrigger,
type EventPayload,
Expand All @@ -37,7 +36,7 @@ import {
type TriggerOptions,
} from "../types";
import { type EventSchemas } from "./EventSchemas";
import { InngestFunction, type AnyInngestFunction } from "./InngestFunction";
import { InngestFunction } from "./InngestFunction";
import { type InngestFunctionReference } from "./InngestFunctionReference";
import {
InngestMiddleware,
Expand Down Expand Up @@ -65,9 +64,6 @@ export type EventsFromOpts<TOpts extends ClientOptions> =
? U
: Record<string, EventPayload>;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type AnyInngest = Inngest<any>;

/**
* A client used to interact with the Inngest API by sending or reacting to
* events.
Expand Down Expand Up @@ -459,7 +455,7 @@ export class Inngest<TOpts extends ClientOptions = ClientOptions> {
TTrigger extends TriggerOptions<TTriggerName>,
TTriggerName extends keyof EventsFromOpts<TOpts> &
string = EventNameFromTrigger<EventsFromOpts<TOpts>, TTrigger>,
THandler extends AnyHandler = Handler<
THandler extends Handler.Any = Handler<
TOpts,
EventsFromOpts<TOpts>,
TTriggerName,
Expand Down Expand Up @@ -665,6 +661,37 @@ export const builtInMiddleware = (<T extends MiddlewareStack>(m: T): T => m)([
}),
]);

/**
* A client used to interact with the Inngest API by sending or reacting to
* events.
*
* To provide event typing, see {@link EventSchemas}.
*
* ```ts
* const inngest = new Inngest({ name: "My App" });
*
* // or to provide event typing too
* const inngest = new Inngest({
* name: "My App",
* schemas: new EventSchemas().fromRecord<{
* "app/user.created": {
* data: { userId: string };
* };
* }>(),
* });
* ```
*
* @public
*/
export namespace Inngest {
/**
* Represents any `Inngest` instance, regardless of generics and
* inference.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Any = Inngest<any>;
}

/**
* A helper type to extract the type of a set of event tooling from a given
* Inngest instance and optionally a trigger.
Expand Down Expand Up @@ -737,7 +764,7 @@ export type GetFunctionInput<
*/
export type GetFunctionOutput<
TFunction extends InvokeTargetFunctionDefinition,
> = TFunction extends AnyInngestFunction
> = TFunction extends InngestFunction.Any
? GetFunctionOutputFromInngestFunction<TFunction>
: TFunction extends InngestFunctionReference.Any
? GetFunctionOutputFromReferenceInngestFunction<TFunction>
Expand All @@ -752,7 +779,7 @@ export type GetFunctionOutput<
* @internal
*/
export type GetFunctionOutputFromInngestFunction<
TFunction extends AnyInngestFunction,
TFunction extends InngestFunction.Any,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
> = TFunction extends InngestFunction<any, any, any, any, infer IHandler>
? IfNever<
Expand Down
15 changes: 7 additions & 8 deletions packages/inngest/src/components/InngestCommHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ import {
type SupportedFrameworkName,
} from "../types";
import { version } from "../version";
import { type AnyInngest } from "./Inngest";
import { type Inngest } from "./Inngest";
import {
type AnyInngestFunction,
type CreateExecutionOptions,
type InngestFunction,
} from "./InngestFunction";
Expand All @@ -70,12 +69,12 @@ export interface ServeHandlerOptions extends RegisterOptions {
/**
* The `Inngest` instance used to declare all functions.
*/
client: AnyInngest;
client: Inngest.Any;

/**
* An array of the functions to serve and register with Inngest.
*/
functions: readonly AnyInngestFunction[];
functions: readonly InngestFunction.Any[];
}

export interface InternalServeHandlerOptions extends ServeHandlerOptions {
Expand Down Expand Up @@ -113,12 +112,12 @@ interface InngestCommHandlerOptions<
* receiving events from the same service, as you can reuse a single
* definition of Inngest.
*/
client: AnyInngest;
client: Inngest.Any;

/**
* An array of the functions to serve and register with Inngest.
*/
functions: readonly AnyInngestFunction[];
functions: readonly InngestFunction.Any[];

/**
* The `handler` is the function your framework requires to handle a
Expand Down Expand Up @@ -307,9 +306,9 @@ export class InngestCommHandler<
* A private collection of just Inngest functions, as they have been passed
* when instantiating the class.
*/
private readonly rawFns: AnyInngestFunction[];
private readonly rawFns: InngestFunction.Any[];

private readonly client: AnyInngest;
private readonly client: Inngest.Any;

/**
* A private collection of functions that are being served. This map is used
Expand Down
9 changes: 3 additions & 6 deletions packages/inngest/src/components/InngestFunction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ import {
NonRetriableError,
type EventPayload,
} from "@local";
import {
InngestFunction,
type AnyInngestFunction,
} from "@local/components/InngestFunction";
import { InngestFunction } from "@local/components/InngestFunction";
import { STEP_INDEXING_SUFFIX } from "@local/components/InngestStepTools";
import {
ExecutionVersion,
Expand Down Expand Up @@ -228,7 +225,7 @@ describe("runFn", () => {
describe("step functions", () => {
const runFnWithStack = (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fn: AnyInngestFunction,
fn: InngestFunction.Any,
stepState: InngestExecutionOptions["stepState"],
opts?: {
executionVersion?: ExecutionVersion;
Expand Down Expand Up @@ -272,7 +269,7 @@ describe("runFn", () => {
const testFn = <
T extends {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fn: AnyInngestFunction;
fn: InngestFunction.Any;
steps: Record<
string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
24 changes: 19 additions & 5 deletions packages/inngest/src/components/InngestFunction.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { internalEvents, queryKeys } from "../helpers/consts";
import { timeStr } from "../helpers/strings";
import {
type AnyHandler,
type ClientOptions,
type EventNameFromTrigger,
type FunctionConfig,
Expand All @@ -19,9 +18,6 @@ import {
import { createV0InngestExecution } from "./execution/v0";
import { createV1InngestExecution } from "./execution/v1";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type AnyInngestFunction = InngestFunction<any, any, any, any, any>;

/**
* A stateless Inngest function, wrapping up function configuration and any
* in-memory steps to run when triggered.
Expand All @@ -41,7 +37,7 @@ export class InngestFunction<
Events,
EventNameFromTrigger<Events, Trigger>
> = FunctionOptions<Events, EventNameFromTrigger<Events, Trigger>>,
THandler extends AnyHandler = Handler<TOpts, Events, keyof Events & string>,
THandler extends Handler.Any = Handler<TOpts, Events, keyof Events & string>,
> {
static stepId = "step";
static failureSuffix = "-failure";
Expand Down Expand Up @@ -217,6 +213,24 @@ export class InngestFunction<
}
}

/**
* A stateless Inngest function, wrapping up function configuration and any
* in-memory steps to run when triggered.
*
* This function can be "registered" to create a handler that Inngest can
* trigger remotely.
*
* @public
*/
export namespace InngestFunction {
/**
* Represents any `InngestFunction` instance, regardless of generics and
* inference.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Any = InngestFunction<any, any, any, any, any>;
}

export type CreateExecutionOptions = {
version: ExecutionVersion;
partialOptions: Omit<InngestExecutionOptions, "client" | "fn">;
Expand Down

0 comments on commit eec41d2

Please sign in to comment.