@saleor/app-sdk
Version:
SDK for building great Saleor Apps
83 lines (77 loc) • 3.5 kB
TypeScript
import { SaleorSchemaVersion, AppManifest, Permission, AsyncWebhookEventType, SyncWebhookEventType, WebhookManifest } from './types.js';
import { AuthData, APL } from './APL/index.js';
import { ASTNode } from 'graphql';
import { v as verifySignatureWithJwks } from './verify-signature-mKf0fpOE.js';
import { c as WebhookError, F as FormatWebhookErrorResult, P as PlatformAdapterInterface, d as WebhookContext } from './saleor-webhook-De0XK_dM.js';
type CreateManifestHandlerOptions<T> = {
manifestFactory(context: {
appBaseUrl: string;
request: T;
/**
* Schema version is optional. During installation, Saleor will send it,
* so manifest can be generated according to the version. But it may
* be also requested from plain GET from the browser, so it may not be available
*/
schemaVersion?: SaleorSchemaVersion;
}): AppManifest | Promise<AppManifest>;
};
type TokenUserPayload = {
email: string;
userPermissions: Permission[];
};
type ProtectedHandlerContext = {
baseUrl: string;
authData: AuthData;
user: TokenUserPayload;
};
interface GenericWebhookConfig<RequestType, Event = AsyncWebhookEventType | SyncWebhookEventType> {
name?: string;
webhookPath: string;
event: Event;
isActive?: boolean;
apl: APL;
onError?(error: WebhookError | Error, request: RequestType): void;
formatErrorResponse?(error: WebhookError | Error, request: RequestType): Promise<FormatWebhookErrorResult>;
query: string | ASTNode;
/**
* Allows to overwrite the default signature verification function.
*
* This is useful for testing purposes, when you want to fabricate a payload, or to opt-out from the default behavior from the library
*/
verifySignatureFn?: typeof verifySignatureWithJwks;
}
declare abstract class GenericSaleorWebhook<TRequestType, TPayload = unknown> {
protected abstract eventType: "async" | "sync";
name: string;
webhookPath: string;
query: string | ASTNode;
event: AsyncWebhookEventType | SyncWebhookEventType;
isActive?: boolean;
apl: APL;
onError: GenericWebhookConfig<TRequestType>["onError"];
formatErrorResponse: GenericWebhookConfig<TRequestType>["formatErrorResponse"];
verifySignatureFn: typeof verifySignatureWithJwks;
private webhookValidator;
protected constructor(configuration: GenericWebhookConfig<TRequestType>);
/** Gets webhook absolute URL based on baseUrl of app
* baseUrl is passed usually from manifest
* baseUrl can include it's own pathname (e.g. http://aws-lambda.com/prod -> has /prod pathname)
* that should be included in full webhook URL, e.g. http://my-webhook.com/prod/api/webhook/order-created */
private getTargetUrl;
/**
* Returns synchronous event manifest for this webhook.
*
* @param baseUrl Base URL used by your application
* @returns WebhookManifest
*/
getWebhookManifest(baseUrl: string): WebhookManifest;
protected prepareRequest<Adapter extends PlatformAdapterInterface<TRequestType>>(adapter: Adapter): Promise<{
result: "callHandler";
context: WebhookContext<TPayload>;
} | {
result: "sendResponse";
response: ReturnType<Adapter["send"]>;
}>;
abstract createHandler(handlerFn: unknown): unknown;
}
export { type CreateManifestHandlerOptions as C, GenericSaleorWebhook as G, type ProtectedHandlerContext as P, type GenericWebhookConfig as a };