picked 0.0.111-next.2 → 0.0.111-next.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/.tsbuildinfo +1 -1
- package/docs/explanation/app-authentication.md +178 -0
- package/docs/explanation/apps.md +169 -0
- package/docs/explanation/conditions-and-the-logic-engine.md +12 -2
- package/docs/explanation/feature-flag-allocation.md +5 -3
- package/docs/how-to/apps/add-a-page-to-the-dashboard.md +257 -0
- package/docs/how-to/apps/add-a-workflow-tool.md +199 -0
- package/docs/how-to/apps/add-an-mcp-server-to-an-app.md +210 -0
- package/docs/how-to/apps/connect-a-third-party-service-with-oauth.md +307 -0
- package/docs/how-to/apps/create-an-app-in-the-dashboard.md +162 -0
- package/docs/how-to/apps/deploy-an-app.md +201 -0
- package/docs/how-to/apps/develop-an-app-locally.md +152 -0
- package/docs/how-to/apps/host-an-app-on-vercel.md +226 -0
- package/docs/how-to/apps/install-an-app.md +116 -0
- package/docs/how-to/apps/link-to-an-installation-from-your-site.md +134 -0
- package/docs/how-to/apps/read-project-data-from-an-app.md +225 -0
- package/docs/how-to/apps/rotate-an-app-key.md +130 -0
- package/docs/how-to/apps/run-code-when-a-workspace-publishes.md +148 -0
- package/docs/how-to/apps/store-data-for-an-installation.md +230 -0
- package/docs/how-to/apps/test-an-app.md +359 -0
- package/docs/how-to/apps/upgrade-or-uninstall-an-app.md +97 -0
- package/docs/how-to/apps/use-an-app-tool-in-a-workflow.md +112 -0
- package/docs/index.md +6 -3
- package/docs/reference/apps/app-kit.md +811 -0
- package/docs/reference/apps/environment.md +116 -0
- package/docs/reference/apps/manifest.md +311 -0
- package/docs/reference/apps/protocol.md +497 -0
- package/docs/reference/dashboard/apps.md +504 -0
- package/docs/reference/dashboard/external-apis.md +5 -0
- package/docs/reference/dashboard/features.mdx +5 -5
- package/docs/reference/dashboard/labels.md +25 -8
- package/docs/reference/dashboard/lead-search.md +6 -6
- package/docs/reference/dashboard/reports.mdx +13 -5
- package/docs/reference/dashboard/workflow-tools.md +14 -0
- package/docs/reference/glossary.md +153 -140
- package/docs/reference/management-api/authentication.md +36 -27
- package/docs/reference/management-api/authorization-and-scopes.md +73 -21
- package/docs/reference/management-api/schema/api-key.md +10 -10
- package/docs/reference/management-api/schema/app.md +726 -0
- package/docs/reference/management-api/schema/auth.md +7 -7
- package/docs/reference/management-api/schema/common.md +54 -35
- package/docs/reference/management-api/schema/feature.md +1 -1
- package/docs/reference/management-api/schema/feedback.md +1 -1
- package/docs/reference/management-api/schema/index.md +35 -20
- package/docs/reference/management-api/schema/label.md +31 -8
- package/docs/reference/management-api/schema/organization.md +66 -66
- package/docs/reference/management-api/schema/project.md +5 -5
- package/docs/reference/management-api/schema/report.md +1 -1
- package/docs/reference/management-api/schema/shopify.md +19 -19
- package/docs/reference/management-api/schema/tenant-branding.md +16 -0
- package/docs/reference/management-api/schema/workflow.md +6 -1
- package/docs/reference/management-api/schema/workspace.md +2 -2
- package/docs/reference/sdk/analytics-events.md +42 -36
- package/docs/reference/sdk/conditions.md +3 -3
- package/docs/reference/sdk/fields.md +5 -4
- package/docs/reference/sdk/index.md +4 -4
- package/docs/reference/sdk/plugins/create-experience.md +11 -9
- package/docs/reference/sdk/plugins/hotjar.md +2 -2
- package/docs/reference/sdk/plugins/sentry.md +2 -2
- package/docs/reference/sdk/plugins/shopify.md +2 -2
- package/docs/reference/sdk/predicates.md +5 -5
- package/docs/reference/sdk/ssr/index.md +4 -3
- package/docs/reference/sdk/ssr/rendering-and-hydration.md +30 -0
- package/docs/reference/sdk/type-generation.md +24 -19
- package/docs/reference/tools/admin-api-client.md +15 -0
- package/docs/reference/tools/cli.md +192 -41
- package/docs/reference/tools/mcp.md +30 -9
- package/docs/tutorials/first-app.md +308 -0
- package/package.json +4 -4
- package/docs/explanation/how-pickedql-works.md +0 -76
- package/docs/how-to/analytics/index.md +0 -324
- package/docs/reference/management-api/schema/analytics.md +0 -75
- package/docs/reference/pickedql/data-sources.md +0 -174
- package/docs/reference/pickedql/dimensions.md +0 -103
- package/docs/reference/pickedql/filtering.md +0 -64
- package/docs/reference/pickedql/index.md +0 -18
- package/docs/reference/pickedql/schema.md +0 -172
- package/docs/reference/pickedql/virtual-functions.md +0 -248
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
---
|
|
2
|
+
type: how-to
|
|
3
|
+
layout: docs
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# How to read project data from an app
|
|
7
|
+
|
|
8
|
+
This guide shows you how to call the Picked GraphQL API from the server of your app, for the
|
|
9
|
+
project of one installation. The example reads the newest leads of the project.
|
|
10
|
+
|
|
11
|
+
You need an app with a server that uses `@picked/app-kit`, and the private key of the app.
|
|
12
|
+
[Build and install your first app](/tutorials/first-app) makes these.
|
|
13
|
+
|
|
14
|
+
## 1. Ask for the scopes
|
|
15
|
+
|
|
16
|
+
Add each scope that your queries need to `scopes` in the manifest. To read leads, ask for
|
|
17
|
+
`organization_leads/read`:
|
|
18
|
+
|
|
19
|
+
```ts [picked.config.ts]
|
|
20
|
+
import { defineConfig } from '@picked/config/define'
|
|
21
|
+
|
|
22
|
+
const endpoint = process.env.APP_PUBLIC_URL ?? 'http://localhost:4103'
|
|
23
|
+
|
|
24
|
+
export default defineConfig({
|
|
25
|
+
app: {
|
|
26
|
+
handle: 'lead-insights',
|
|
27
|
+
name: 'Lead Insights',
|
|
28
|
+
endpoint,
|
|
29
|
+
scopes: ['organization_leads/read'],
|
|
30
|
+
slots: [{ location: 'app-home', url: `${endpoint}/`, title: 'Recent leads', height: 320 }],
|
|
31
|
+
},
|
|
32
|
+
})
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
The person who installs the app sees these scopes and accepts them. That person must hold
|
|
36
|
+
each scope on the project. Each token of the installation carries the accepted scopes, and
|
|
37
|
+
no other scope. [Scopes](/reference/apps/manifest#scopes) tells which names are valid.
|
|
38
|
+
|
|
39
|
+
When you add a scope to an app that is installed, deploy a new version. Each installation
|
|
40
|
+
keeps the scopes of its old version until a person upgrades it and accepts the new scope.
|
|
41
|
+
Refer to [Upgrade an app](/how-to/apps/upgrade-or-uninstall-an-app#upgrade-an-app).
|
|
42
|
+
|
|
43
|
+
## 2. Write the query
|
|
44
|
+
|
|
45
|
+
Write the GraphQL operation with `graphql` from `@picked/admin-api-client`. Install the
|
|
46
|
+
package in the app directory first:
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
npm install @picked/admin-api-client
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
`graphql` makes a typed document from the operation. Use the `projectId` of the request, not
|
|
53
|
+
an ID from the page:
|
|
54
|
+
|
|
55
|
+
```ts [src/leads.ts]
|
|
56
|
+
import { graphql, type ResultOf } from '@picked/admin-api-client'
|
|
57
|
+
import { installationClientFor, type InstallationRef } from '@picked/app-kit/server'
|
|
58
|
+
|
|
59
|
+
const RecentLeadsDocument = graphql(`
|
|
60
|
+
query RecentLeads($projectId: ID!, $first: Int!) {
|
|
61
|
+
project(input: { id: $projectId }) {
|
|
62
|
+
leads(first: $first) {
|
|
63
|
+
matchCount
|
|
64
|
+
nodes {
|
|
65
|
+
id
|
|
66
|
+
createdAt
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
`)
|
|
72
|
+
|
|
73
|
+
export async function recentLeads(
|
|
74
|
+
privateKey: string,
|
|
75
|
+
installation: InstallationRef,
|
|
76
|
+
sessionToken?: string | null,
|
|
77
|
+
): Promise<ResultOf<typeof RecentLeadsDocument>> {
|
|
78
|
+
const client = installationClientFor(installation, { privateKey, sessionToken })
|
|
79
|
+
return client.request(RecentLeadsDocument, { projectId: installation.projectId, first: 10 })
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
`client.request` sends the operation to `<apiUrl>/api/graphql` with a token of the
|
|
84
|
+
installation. It returns `data`, with the type of the document. If the answer has errors, it
|
|
85
|
+
throws an `AdminApiResponseError`. The message of this error has the message of each error,
|
|
86
|
+
and its `codes` has the code of each error. A query outside the scopes of the installation
|
|
87
|
+
fails. A request that takes more than 30 seconds stops with an error.
|
|
88
|
+
|
|
89
|
+
[The Management API](/reference/management-api/schema) lists each operation and the scope
|
|
90
|
+
that it needs.
|
|
91
|
+
|
|
92
|
+
## 3. Call the API from a request from Picked
|
|
93
|
+
|
|
94
|
+
A lifecycle event, an app event, a tool call and an MCP request each contain a token. The
|
|
95
|
+
token is valid for one hour and carries the scopes of the installation. Give the request to
|
|
96
|
+
`installationClientFor`, and the client uses that token:
|
|
97
|
+
|
|
98
|
+
```ts [src/server.ts]
|
|
99
|
+
import { createAppHandler, readAppEnvironment } from '@picked/app-kit/server'
|
|
100
|
+
import { serve } from '@picked/app-kit/node'
|
|
101
|
+
import { recentLeads } from './leads'
|
|
102
|
+
|
|
103
|
+
const port = process.env.APP_PORT ?? '4103'
|
|
104
|
+
const { privateKey, pickedKeys } = readAppEnvironment(process.env, port)
|
|
105
|
+
|
|
106
|
+
const handler = createAppHandler({
|
|
107
|
+
pickedKeys,
|
|
108
|
+
async onInstall(event) {
|
|
109
|
+
console.log(await recentLeads(privateKey, event))
|
|
110
|
+
},
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
serve(handler, Number(port))
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## 4. Call the API from a page request
|
|
117
|
+
|
|
118
|
+
A request from a page of your app does not come from Picked, so it contains no token. It
|
|
119
|
+
contains the session token of the person, in the `Authorization` header. Verify the session
|
|
120
|
+
with `readSession`. Then give the raw session token to the client with `sessionTokenOf`:
|
|
121
|
+
|
|
122
|
+
```ts [src/server.ts]
|
|
123
|
+
import { json, readSession, sessionTokenOf } from '@picked/app-kit/server'
|
|
124
|
+
|
|
125
|
+
// In the `fallback` of `createAppHandler`:
|
|
126
|
+
if (pathname === '/api/leads') {
|
|
127
|
+
const session = await readSession(request, pickedKeys)
|
|
128
|
+
if (session === null) return json({ error: 'unauthenticated' }, 401)
|
|
129
|
+
try {
|
|
130
|
+
return json(await recentLeads(privateKey, session, sessionTokenOf(request)))
|
|
131
|
+
} catch (error) {
|
|
132
|
+
return json({ error: error instanceof Error ? error.message : String(error) }, 502)
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
The client sends `appInstallationTokenCreate` with the session token. Picked then gives a
|
|
138
|
+
token for the installation. The client signs the request with the private key of the app.
|
|
139
|
+
|
|
140
|
+
The page sends the session token from `connect()`:
|
|
141
|
+
|
|
142
|
+
```js
|
|
143
|
+
import { connect } from '/app-kit/frame.js'
|
|
144
|
+
|
|
145
|
+
const context = await connect()
|
|
146
|
+
const response = await fetch('/api/leads', {
|
|
147
|
+
headers: { authorization: `Bearer ${context.sessionToken}` },
|
|
148
|
+
})
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
A session token expires 10 minutes after Picked makes it. To get a new one, call `connect()`
|
|
152
|
+
again. [How to add a page to the dashboard](/how-to/apps/add-a-page-to-the-dashboard) shows
|
|
153
|
+
the full page.
|
|
154
|
+
|
|
155
|
+
## 5. Call the API outside a request
|
|
156
|
+
|
|
157
|
+
To call Picked when no request comes in, for example at an interval that you set, set
|
|
158
|
+
`offlineAccess: true` in the manifest:
|
|
159
|
+
|
|
160
|
+
```ts [picked.config.ts]
|
|
161
|
+
export default defineConfig({
|
|
162
|
+
app: {
|
|
163
|
+
handle: 'lead-insights',
|
|
164
|
+
name: 'Lead Insights',
|
|
165
|
+
endpoint,
|
|
166
|
+
scopes: ['organization_leads/read'],
|
|
167
|
+
offlineAccess: true,
|
|
168
|
+
},
|
|
169
|
+
})
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
The person who installs or upgrades the app accepts this access in the
|
|
173
|
+
[consent window](/reference/dashboard/apps#consent-window).
|
|
174
|
+
|
|
175
|
+
Keep the `installationId` and `apiUrl` of each installation, for example from the
|
|
176
|
+
`installation.created` event. Then make a client without a token:
|
|
177
|
+
|
|
178
|
+
```ts [src/sync.ts]
|
|
179
|
+
import { graphql } from '@picked/admin-api-client'
|
|
180
|
+
import { createInstallationClient } from '@picked/app-kit/server'
|
|
181
|
+
|
|
182
|
+
const LeadCountDocument = graphql(`
|
|
183
|
+
query LeadCount($id: ID!) {
|
|
184
|
+
project(input: { id: $id }) {
|
|
185
|
+
leads(first: 1) {
|
|
186
|
+
matchCount
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
`)
|
|
191
|
+
|
|
192
|
+
const client = createInstallationClient({
|
|
193
|
+
apiUrl: 'https://api.picked.app',
|
|
194
|
+
installationId: 'appi_k3v9x2m1q8zt',
|
|
195
|
+
privateKey: process.env.PICKED_APP_PRIVATE_KEY ?? '',
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
const data = await client.request(LeadCountDocument, { id: 'proj_4kd9a2' })
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
The client gets a token with `appInstallationTokenCreate` at any time. It gets a new token
|
|
202
|
+
60 seconds before the old token expires.
|
|
203
|
+
|
|
204
|
+
Without `offlineAccess`, Picked refuses that request.
|
|
205
|
+
[The `appInstallationTokenCreate` mutation](/reference/apps/protocol#the-appinstallationtokencreate-mutation)
|
|
206
|
+
gives the error message.
|
|
207
|
+
|
|
208
|
+
## 6. Know when a token stops
|
|
209
|
+
|
|
210
|
+
A token of the installation stops in these conditions:
|
|
211
|
+
|
|
212
|
+
- One hour after Picked makes it.
|
|
213
|
+
- When a person removes the app from the project.
|
|
214
|
+
|
|
215
|
+
## Related
|
|
216
|
+
|
|
217
|
+
- [`@picked/app-kit`](/reference/apps/app-kit#installation-client) — `installationClientFor`,
|
|
218
|
+
`createInstallationClient` and `request`.
|
|
219
|
+
- [App protocol](/reference/apps/protocol#installation-tokens) — installation tokens and
|
|
220
|
+
`appInstallationTokenCreate`.
|
|
221
|
+
- [About app authentication](/explanation/app-authentication) — why a page request needs a
|
|
222
|
+
session token.
|
|
223
|
+
- [App manifest](/reference/apps/manifest#offlineaccess) — `scopes` and `offlineAccess`.
|
|
224
|
+
- [How to store data for an installation](/how-to/apps/store-data-for-an-installation) —
|
|
225
|
+
keep data in labels with the same client.
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
---
|
|
2
|
+
type: how-to
|
|
3
|
+
layout: docs
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# How to rotate an app key
|
|
7
|
+
|
|
8
|
+
This guide shows you how to replace the private key of an app with a new key. The app
|
|
9
|
+
continues to work during the change.
|
|
10
|
+
|
|
11
|
+
Before you start, the app must have one public key, and its server must use the private key of
|
|
12
|
+
that public key. You must have the scope `organization_apps/update` in the organization
|
|
13
|
+
of the app.
|
|
14
|
+
|
|
15
|
+
An app holds two public keys at most. Picked accepts a `picked-app-assertion` token from
|
|
16
|
+
each of them. So you add the new key first, move the server to it, and then remove the
|
|
17
|
+
old key.
|
|
18
|
+
|
|
19
|
+
## 1. Make a new key
|
|
20
|
+
|
|
21
|
+
Make a private key and its public key:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npx picked app keys generate
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
A new app key pair. Keep the private key secret; it is printed once:
|
|
29
|
+
|
|
30
|
+
PICKED_APP_PRIVATE_KEY=MC4CAQAwBQYDK2VwBCIEI…
|
|
31
|
+
|
|
32
|
+
Public key: 3fJ0b…
|
|
33
|
+
Key id: Qm9yZ2…
|
|
34
|
+
|
|
35
|
+
Set PICKED_APP_PRIVATE_KEY on the app server and where you run `picked app deploy`,
|
|
36
|
+
which registers the public key with the app.
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Copy the `PICKED_APP_PRIVATE_KEY=…` line to your secret store. The CLI shows it one time
|
|
40
|
+
only.
|
|
41
|
+
|
|
42
|
+
## 2. Register the new public key
|
|
43
|
+
|
|
44
|
+
Use one of the two methods that follow.
|
|
45
|
+
|
|
46
|
+
### With the CLI
|
|
47
|
+
|
|
48
|
+
Set the new private key in the shell, then deploy the app:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
export PICKED_APP_PRIVATE_KEY='MC4CAQAwBQYDK2VwBCIEI…'
|
|
52
|
+
npx picked app deploy --org acme
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
The CLI registers the public key of the new private key. If the manifest has no
|
|
56
|
+
change, the CLI writes no version. Refer to
|
|
57
|
+
[How to deploy an app](/how-to/apps/deploy-an-app).
|
|
58
|
+
|
|
59
|
+
### In the dashboard
|
|
60
|
+
|
|
61
|
+
1. In the organization switcher, choose **Apps**.
|
|
62
|
+
2. Find the app.
|
|
63
|
+
3. Choose the app.
|
|
64
|
+
4. In **Public keys**, choose **Add key**.
|
|
65
|
+
5. In the window **`Add a public key`**, put the `Public key` value in the field **Public key**.
|
|
66
|
+
6. Choose **Add key**.
|
|
67
|
+
|
|
68
|
+
To make the keys in the dashboard, choose **`Generate a key pair`** in place of steps 5 and 6. The
|
|
69
|
+
dashboard registers the public key. The window shows the public key in the field **Public key**,
|
|
70
|
+
and the private key in the field `PICKED_APP_PRIVATE_KEY` until you choose **Done**. Copy it with
|
|
71
|
+
**`Copy PICKED_APP_PRIVATE_KEY`** before you choose **Done**.
|
|
72
|
+
|
|
73
|
+
The app now holds two keys, and **Public keys** shows
|
|
74
|
+
`An app holds two keys at most. Remove one to add another.`
|
|
75
|
+
|
|
76
|
+
If the app holds two keys already, the new key fails with
|
|
77
|
+
`An app holds at most 2 public keys; remove one before adding another`.
|
|
78
|
+
|
|
79
|
+
## 3. Move the server to the new key
|
|
80
|
+
|
|
81
|
+
1. Set `PICKED_APP_PRIVATE_KEY` on the server to the new private key.
|
|
82
|
+
2. Deploy the server again.
|
|
83
|
+
3. Set the new private key where your CI runs `picked app deploy`.
|
|
84
|
+
|
|
85
|
+
Each later `picked app deploy` keeps the new key.
|
|
86
|
+
|
|
87
|
+
:::warning
|
|
88
|
+
Remove the old private key from your CI before you remove the old key from the app. Each
|
|
89
|
+
`picked app deploy` registers the key that it gets. So the old private key in CI adds the
|
|
90
|
+
old key to the app again.
|
|
91
|
+
:::
|
|
92
|
+
|
|
93
|
+
:::note
|
|
94
|
+
`startAuthorization` from `@picked/app-kit/oauth` encrypts the OAuth state with a key from the
|
|
95
|
+
private key. An OAuth flow that starts before the change and ends after it fails. The person
|
|
96
|
+
starts the flow again.
|
|
97
|
+
:::
|
|
98
|
+
|
|
99
|
+
## 4. Remove the old key
|
|
100
|
+
|
|
101
|
+
1. On the page of the app, find the old key under **Public keys**. The keys show the oldest
|
|
102
|
+
first, so the old key is `Key 1, added <date>`. The date is the day that you added it.
|
|
103
|
+
2. Choose **Remove** below that key.
|
|
104
|
+
3. Read the warning in the window `Remove key 1, added <date>?`.
|
|
105
|
+
4. Choose **Remove**.
|
|
106
|
+
|
|
107
|
+
After this, each `picked-app-assertion` token that the old key signs fails. A server that
|
|
108
|
+
still uses the old key cannot get a new installation token. An installation token that the
|
|
109
|
+
server got before stays valid until it expires.
|
|
110
|
+
|
|
111
|
+
## API and MCP
|
|
112
|
+
|
|
113
|
+
You can also do this task with the [Management API](/reference/management-api/schema), the [MCP server](/reference/tools/mcp), or code that you send to [`picked_run`](/reference/tools/mcp-run).
|
|
114
|
+
|
|
115
|
+
| Task | GraphQL | MCP tool | Run |
|
|
116
|
+
| --- | --- | --- | --- |
|
|
117
|
+
| Register the new public key | [`appPublicKeyAdd`](/reference/management-api/schema/app#apppublickeyadd) | None | None |
|
|
118
|
+
| Remove the old key | [`appPublicKeyRemove`](/reference/management-api/schema/app#apppublickeyremove) | None | None |
|
|
119
|
+
|
|
120
|
+
`appPublicKeyRemove` takes the key ID as `kid`. The `publicKeys` field of
|
|
121
|
+
[`app`](/reference/management-api/schema/app#app) lists the keys of the app.
|
|
122
|
+
|
|
123
|
+
## Related
|
|
124
|
+
|
|
125
|
+
- [Public keys of an app](/reference/apps/protocol#public-keys-of-an-app) — the key format,
|
|
126
|
+
the key ID and the limit.
|
|
127
|
+
- [About app authentication](/explanation/app-authentication#two-keys-for-each-app) — why
|
|
128
|
+
an app holds two keys.
|
|
129
|
+
- [App environment variables](/reference/apps/environment) — `PICKED_APP_PRIVATE_KEY` on
|
|
130
|
+
the server and in the CLI.
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
---
|
|
2
|
+
type: how-to
|
|
3
|
+
layout: docs
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# How to run code when a workspace publishes
|
|
7
|
+
|
|
8
|
+
This guide shows you how to run code on your server each time a workspace publishes in a
|
|
9
|
+
project that has your app. For example, you can start a build of a site, or clear a cache.
|
|
10
|
+
|
|
11
|
+
You need an app with a server that uses `@picked/app-kit`, and a public `endpoint`.
|
|
12
|
+
[Build and install your first app](/tutorials/first-app) makes these.
|
|
13
|
+
|
|
14
|
+
## 1. Subscribe to the event
|
|
15
|
+
|
|
16
|
+
Add `workspace.published` to `events` in the manifest. The manifest must have an
|
|
17
|
+
`endpoint`, because Picked sends each event to `<endpoint>/events`.
|
|
18
|
+
|
|
19
|
+
```ts [picked.config.ts]
|
|
20
|
+
import { defineConfig } from '@picked/config/define'
|
|
21
|
+
|
|
22
|
+
export default defineConfig({
|
|
23
|
+
app: {
|
|
24
|
+
handle: 'my-app',
|
|
25
|
+
name: 'My App',
|
|
26
|
+
endpoint: 'https://my-app.example.com',
|
|
27
|
+
events: ['workspace.published'],
|
|
28
|
+
},
|
|
29
|
+
})
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
`workspace.published` is the only event.
|
|
33
|
+
|
|
34
|
+
## 2. Handle the event
|
|
35
|
+
|
|
36
|
+
Give `createAppHandler` an `onEvent` function. `createAppHandler` checks that Picked signed
|
|
37
|
+
the request, then calls `onEvent` with the event:
|
|
38
|
+
|
|
39
|
+
```ts [src/server.ts]
|
|
40
|
+
import { createAppHandler, readAppEnvironment } from '@picked/app-kit/server'
|
|
41
|
+
import { serve } from '@picked/app-kit/node'
|
|
42
|
+
|
|
43
|
+
const port = process.env.APP_PORT ?? '4103'
|
|
44
|
+
const { pickedKeys } = readAppEnvironment(process.env, port)
|
|
45
|
+
|
|
46
|
+
const handler = createAppHandler({
|
|
47
|
+
pickedKeys,
|
|
48
|
+
async onEvent(event) {
|
|
49
|
+
if (event.type !== 'workspace.published') return
|
|
50
|
+
console.log(
|
|
51
|
+
`Project ${event.projectId} published version ${event.workspaceVersionId}`,
|
|
52
|
+
`of workspace ${event.workspaceId} at ${event.occurredAt}`,
|
|
53
|
+
)
|
|
54
|
+
},
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
serve(handler, Number(port))
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Use these fields of the event:
|
|
61
|
+
|
|
62
|
+
- **`workspaceId`.** The workspace that was published.
|
|
63
|
+
- **`workspaceVersionId`.** The version that Picked wrote when it published the workspace.
|
|
64
|
+
- **`eventId`.** The ID of the event. Step 3 shows how to use it.
|
|
65
|
+
- **`token`.** A token for the Picked API, with the scopes of the installation. It expires
|
|
66
|
+
after one hour.
|
|
67
|
+
|
|
68
|
+
[App events](/reference/apps/protocol#app-events) gives each field.
|
|
69
|
+
|
|
70
|
+
`createAppHandler` answers `200` after `onEvent` resolves. Picked waits 15 seconds at most
|
|
71
|
+
for the answer, resends included. Keep the work in `onEvent` short.
|
|
72
|
+
|
|
73
|
+
The workspace publishes also when your server fails. Picked does not show your answer to the
|
|
74
|
+
person who publishes.
|
|
75
|
+
|
|
76
|
+
## 3. Skip an event that you already handled
|
|
77
|
+
|
|
78
|
+
Picked can send one event more than one time. It sends the event again after a `408`, a
|
|
79
|
+
`429` or a `5xx` answer, or when it gets no answer. Picked can also send the same event again at a
|
|
80
|
+
later time. Each send has the same `eventId`.
|
|
81
|
+
|
|
82
|
+
Keep the `eventId` of the last event that you handled. If an event has that `eventId`, do
|
|
83
|
+
nothing. The Vercel app of Picked keeps it in a label of the installation:
|
|
84
|
+
|
|
85
|
+
```ts [src/server.ts]
|
|
86
|
+
import { createAppHandler, installationClientFor, readAppEnvironment } from '@picked/app-kit/server'
|
|
87
|
+
import { serve } from '@picked/app-kit/node'
|
|
88
|
+
|
|
89
|
+
const port = process.env.APP_PORT ?? '4103'
|
|
90
|
+
const { pickedKeys, privateKey } = readAppEnvironment(process.env, port)
|
|
91
|
+
|
|
92
|
+
const LAST_EVENT = { namespace: 'my-app', key: 'last-event' }
|
|
93
|
+
|
|
94
|
+
const handler = createAppHandler({
|
|
95
|
+
pickedKeys,
|
|
96
|
+
async onEvent(event) {
|
|
97
|
+
const client = installationClientFor(event, { privateKey })
|
|
98
|
+
const last = await client.labels.get(LAST_EVENT.namespace, LAST_EVENT.key)
|
|
99
|
+
if (last?.value === event.eventId) return
|
|
100
|
+
|
|
101
|
+
await client.labels.set([{ ...LAST_EVENT, type: 'text', value: event.eventId }])
|
|
102
|
+
await startBuild(event.projectId, event.workspaceVersionId)
|
|
103
|
+
},
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
async function startBuild(projectId: string, workspaceVersionId: string): Promise<void> {
|
|
107
|
+
await fetch('https://builds.example.com/api/builds', {
|
|
108
|
+
method: 'POST',
|
|
109
|
+
headers: { 'content-type': 'application/json' },
|
|
110
|
+
body: JSON.stringify({ projectId, workspaceVersionId }),
|
|
111
|
+
})
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
serve(handler, Number(port))
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
`installationClientFor` uses the `token` of the event. That token can read and write the
|
|
118
|
+
labels of the installation without a scope. To read other project data, add the scopes that
|
|
119
|
+
you need to `scopes` in the manifest.
|
|
120
|
+
[How to read project data from an app](/how-to/apps/read-project-data-from-an-app) shows
|
|
121
|
+
how.
|
|
122
|
+
|
|
123
|
+
## 4. Deploy and upgrade
|
|
124
|
+
|
|
125
|
+
1. Deploy a new version of the manifest.
|
|
126
|
+
2. Upgrade each installation to that version.
|
|
127
|
+
3. Publish a workspace in a project that has the app.
|
|
128
|
+
|
|
129
|
+
Picked sends an event only to an installation whose version lists the event in `events`.
|
|
130
|
+
An installation that uses an earlier version gets no events until it is upgraded.
|
|
131
|
+
|
|
132
|
+
## Add a button to the publish modal
|
|
133
|
+
|
|
134
|
+
To let a person start the work by hand, add a page to the publish modal.
|
|
135
|
+
Declare a slot with the location `workspace-postpublish-actions`. The page shows in the
|
|
136
|
+
modal after Picked publishes the workspace. The Vercel app of Picked shows a
|
|
137
|
+
`Deploy <project>` button there for each Vercel project that it deploys.
|
|
138
|
+
|
|
139
|
+
[How to add a page to the dashboard](/how-to/apps/add-a-page-to-the-dashboard) shows how.
|
|
140
|
+
|
|
141
|
+
## Related
|
|
142
|
+
|
|
143
|
+
- [App protocol](/reference/apps/protocol#app-events) — the body and the headers of an app
|
|
144
|
+
event.
|
|
145
|
+
- [App manifest](/reference/apps/manifest#events) — the `events` field.
|
|
146
|
+
- [How to store data for an installation](/how-to/apps/store-data-for-an-installation) —
|
|
147
|
+
labels of an installation.
|
|
148
|
+
- [How to publish a workspace](/how-to/publish-a-workspace) — what Picked does when it publishes a workspace.
|