-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcreate-app-register-handler.ts
36 lines (33 loc) · 1.51 KB
/
create-app-register-handler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { RegisterActionHandler } from "@/handlers/actions/register-action-handler";
import { GenericCreateAppRegisterHandlerOptions } from "@/handlers/shared/create-app-register-handler-types";
import { WebApiAdapter, WebApiHandler, WebApiHandlerInput } from "./platform-adapter";
export type CreateAppRegisterHandlerOptions =
GenericCreateAppRegisterHandlerOptions<WebApiHandlerInput>;
/**
* Returns API route handler for Web API compatible request handlers
* (examples: Next.js app router, hono, deno, etc.)
* that use signature: (req: Request) => Response
* where Request and Response are Fetch API objects
*
* Handler is for register endpoint that is called by Saleor when installing the app
*
* It verifies the request and stores `app_token` from Saleor
* in APL and along with all required AuthData fields (jwks, saleorApiUrl, ...)
*
* **Recommended path**: `/api/register`
* (configured in manifest handler)
*
* To learn more check Saleor docs
* @see {@link https://docs.saleor.io/developer/extending/apps/architecture/app-requirements#register-url}
*
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Response}
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Request}
* */
export const createAppRegisterHandler =
(config: CreateAppRegisterHandlerOptions): WebApiHandler =>
async (req) => {
const adapter = new WebApiAdapter(req);
const useCase = new RegisterActionHandler(adapter);
const result = await useCase.handleAction(config);
return adapter.send(result);
};