Skip to content

Commit

Permalink
Add Handler factories (#61)
Browse files Browse the repository at this point in the history
* Add createManifestHandler

* Add register handler factory

* Add docs to handlers factory

* Improve tsup configuration (select entry points)

* Reexport symbols from index

* Improve tests
  • Loading branch information
lkostrowski authored Sep 21, 2022
1 parent df504ec commit 0352356
Show file tree
Hide file tree
Showing 14 changed files with 632 additions and 61 deletions.
75 changes: 75 additions & 0 deletions docs/api-handlers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Api Handlers

Saleor Apps are meant to work in serverless environment, where Cloud Functions are the foundations of server-side code.

Currently, Saleor heavily relies on Next.js, but in the future, other platforms will be supported.

## Required handlers

Saleor requires 2 endpoints to be available for a standalone app:

- Manifest endpoint - Returns JSON object with app properties, like its name or permissions. [Read more](https://docs.saleor.io/docs/3.x/developer/extending/apps/manifest)
- Register endpoint - During the installation process, Saleor sends `POST` request with auth token to this endpoint. [Read more](https://docs.saleor.io/docs/3.x/developer/extending/apps/installing-apps#installation-using-graphql-api)

## Api handlers built-in SDK

To hide Saleor internal logic, app-sdk provides handlers factories. They should work with minimal configuration, leaving
App creators space for domain logic.

### Manifest handler factory

Example usage of manifest handler in Next.js

```typescript
// pages/api/manifest.ts

import { createManifestHandler } from "@app-sdk/handlers/next";

export default createManifestHandler({
manifestFactory(context) {
return {
name: "My Saleor App",
tokenTargetUrl: `${context.appBaseUrl}/api/register`,
appUrl: context.appBaseUrl,
permissions: [],
id: "my-saleor-app",
version: "1",
};
},
});
```

Options provided to handler factory

```typescript
type CreateManifestHandlerOptions = {
manifestFactory(context: { appBaseUrl: string }): AppManifest;
};
```

See [source](./src/handlers/next/create-manifest-handler.ts) for more details. See [manifest](../src/types.ts) too.

### App register handler factory

Example usage of app register handler in Next.js

```typescript
// pages/api/register.ts

import { createAppRegisterHandler } from "@app-sdk/handlers/next";
import { VercelAPL } from "./vercel-apl";

export default createAppRegisterHandler({
apl: new VercelAPL(),
});
```

Options provided to handler factory

```typescript
export type CreateAppRegisterHandlerOptions = {
apl: APL;
};
```

See [APL](./apl.md) for details what is Auth Persistence Layer in Saleor apps
16 changes: 12 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"main": "index.js",
"scripts": {
"prepublishOnly": "pnpm build",
"watch": "tsup-node src/* --format esm,cjs --dts --watch",
"build": "tsup-node src/* --format esm,cjs --dts && clear-package-json package.json -o dist/package.json --fields publishConfig",
"watch": "tsup --watch",
"build": "tsup && clear-package-json package.json -o dist/package.json --fields publishConfig",
"clean": "rm -rf ./dist/*",
"test": "vitest",
"test:ci": "CI=true vitest --coverage",
Expand All @@ -20,19 +20,22 @@
"license": "ISC",
"peerDependencies": {
"react": ">=17",
"react-dom": ">=17"
"react-dom": ">=17",
"next": "^12"
},
"dependencies": {
"debug": "^4.3.4",
"fast-glob": "^3.2.11",
"graphql": "^16.6.0",
"jose": "^4.9.2",
"node-fetch": "^3.2.10",
"retes": "^0.32.0",
"retes": "^0.33.0",
"uuid": "^8.3.2"
},
"devDependencies": {
"@vitest/coverage-c8": "^0.23.2",
"node-mocks-http": "^1.11.0",
"next": "^12.3.0",
"@testing-library/dom": "^8.17.1",
"@testing-library/react": "^13.4.0",
"@types/debug": "^4.1.7",
Expand Down Expand Up @@ -98,6 +101,11 @@
"import": "./app-bridge/index.mjs",
"require": "./app-bridge/index.js"
},
"./handlers/next": {
"types": "./handlers/next/index.d.ts",
"import": "./handlers/next/index.mjs",
"require": "./handlers/next/index.js"
},
".": {
"types": "./index.d.ts",
"import": "./index.mjs",
Expand Down
Loading

0 comments on commit 0352356

Please sign in to comment.