typed-forge-bridge
A small TypeScript library which introduces end-to-end type-safety into your Atlassian Forge Apps!
Usage
Centrally define what the API of your FaaS backend is supposed to look like:
import { ForgeEndpoint } from "@seibert/typed-forge-bridge";
type User = {
id: string,
name: string,
email: string,
};
type GetUserParams = {
id: string,
};
type CreateUserParams = {
name: string,
email: string,
};
export const apiDefinition = {
getUser: new ForgeEndpoint<GetUserParams, User>(),
createUser: new ForgeEndpoint<CreateUserParams, void>(),
};
We can now use this definition to infer the types of the resolver's handler functions.
import Resolver from "@forge/resolver";
import { define } from "@seibert/typed-forge-bridge";
import { apiDefinition } from "@shared/types";
const resolver = new Resolver();
define<typeof apiDefinition>(resolver, {
getUser: async ({ payload, context }) => {
return user;
},
createUser: async ({ payload }) => {
}
});
export const handler = resolver.getDefinitions();
And on the client-side we can infer a fully typed API client for the FaaS backend.
import { invoke } from "@forge/bridge";
import { buildClient } from "@seibert/typed-forge-bridge";
import { apiDefinition } from "@shared/types";
export const bridgeClient = buildClient(apiDefinition, invoke);
function CreateUserButton() {
return <button
onClick={ async () => {
// the method createUser expects CreateUserParams and returns void as
// defined in apiDefinition
await bridgeClient.createUser({
name: "Max Mustermann",
email: "max@mustermann.com",
});
} }
>
create user
</button>;
}