@payloadcms/sdk 0.0.1-beta.0 → 3.58.0
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/README.md +191 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
# Payload SDK
|
|
2
|
+
|
|
3
|
+
Package to allow querying Payload REST API in a fully type safe way. Has support for all necessary operations, including auth, type safe `select`, `populate`, `joins` properties and simplified file uploading. Its interface is _very_ similar to the Local API.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { PayloadSDK } from '@payloadcms/sdk'
|
|
7
|
+
import type { Config } from './payload-types'
|
|
8
|
+
|
|
9
|
+
// Pass your config from generated types as generic
|
|
10
|
+
const sdk = new PayloadSDK<Config>({
|
|
11
|
+
baseURL: 'https://example.com/api',
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
// Find operation
|
|
15
|
+
const posts = await sdk.find({
|
|
16
|
+
collection: 'posts',
|
|
17
|
+
draft: true,
|
|
18
|
+
limit: 10,
|
|
19
|
+
locale: 'en',
|
|
20
|
+
page: 1,
|
|
21
|
+
where: { _status: { equals: 'published' } },
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
// Find by ID operation
|
|
25
|
+
const posts = await sdk.findByID({
|
|
26
|
+
id,
|
|
27
|
+
collection: 'posts',
|
|
28
|
+
draft: true,
|
|
29
|
+
locale: 'en',
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
// Auth login operation
|
|
33
|
+
const result = await sdk.login({
|
|
34
|
+
collection: 'users',
|
|
35
|
+
data: {
|
|
36
|
+
email: 'dev@payloadcms.com',
|
|
37
|
+
password: '12345',
|
|
38
|
+
},
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
// Create operation
|
|
42
|
+
const result = await sdk.create({
|
|
43
|
+
collection: 'posts',
|
|
44
|
+
data: { text: 'text' },
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
// Create operation with a file
|
|
48
|
+
// `file` can be either a Blob | File object or a string URL
|
|
49
|
+
const result = await sdk.create({ collection: 'media', file, data: {} })
|
|
50
|
+
|
|
51
|
+
// Count operation
|
|
52
|
+
const result = await sdk.count({ collection: 'posts', where: { id: { equals: post.id } } })
|
|
53
|
+
|
|
54
|
+
// Update (by ID) operation
|
|
55
|
+
const result = await sdk.update({
|
|
56
|
+
collection: 'posts',
|
|
57
|
+
id: post.id,
|
|
58
|
+
data: {
|
|
59
|
+
text: 'updated-text',
|
|
60
|
+
},
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
// Update (bulk) operation
|
|
64
|
+
const result = await sdk.update({
|
|
65
|
+
collection: 'posts',
|
|
66
|
+
where: {
|
|
67
|
+
id: {
|
|
68
|
+
equals: post.id,
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
data: { text: 'updated-text-bulk' },
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
// Delete (by ID) operation
|
|
75
|
+
const result = await sdk.delete({ id: post.id, collection: 'posts' })
|
|
76
|
+
|
|
77
|
+
// Delete (bulk) operation
|
|
78
|
+
const result = await sdk.delete({ where: { id: { equals: post.id } }, collection: 'posts' })
|
|
79
|
+
|
|
80
|
+
// Find Global operation
|
|
81
|
+
const result = await sdk.findGlobal({ slug: 'global' })
|
|
82
|
+
|
|
83
|
+
// Update Global operation
|
|
84
|
+
const result = await sdk.updateGlobal({ slug: 'global', data: { text: 'some-updated-global' } })
|
|
85
|
+
|
|
86
|
+
// Auth Login operation
|
|
87
|
+
const result = await sdk.login({
|
|
88
|
+
collection: 'users',
|
|
89
|
+
data: { email: 'dev@payloadcms.com', password: '123456' },
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
// Auth Me operation
|
|
93
|
+
const result = await sdk.me(
|
|
94
|
+
{ collection: 'users' },
|
|
95
|
+
{
|
|
96
|
+
headers: {
|
|
97
|
+
Authorization: `JWT ${user.token}`,
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
// Auth Refresh Token operation
|
|
103
|
+
const result = await sdk.refreshToken(
|
|
104
|
+
{ collection: 'users' },
|
|
105
|
+
{ headers: { Authorization: `JWT ${user.token}` } },
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
// Auth Forgot Password operation
|
|
109
|
+
const result = await sdk.forgotPassword({
|
|
110
|
+
collection: 'users',
|
|
111
|
+
data: { email: user.email },
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
// Auth Reset Password operation
|
|
115
|
+
const result = await sdk.resetPassword({
|
|
116
|
+
collection: 'users',
|
|
117
|
+
data: { password: '1234567', token: resetPasswordToken },
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
// Find Versions operation
|
|
121
|
+
const result = await sdk.findVersions({
|
|
122
|
+
collection: 'posts',
|
|
123
|
+
where: { parent: { equals: post.id } },
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
// Find Version by ID operation
|
|
127
|
+
const result = await sdk.findVersionByID({ collection: 'posts', id: version.id })
|
|
128
|
+
|
|
129
|
+
// Restore Version operation
|
|
130
|
+
const result = await sdk.restoreVersion({
|
|
131
|
+
collection: 'posts',
|
|
132
|
+
id,
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
// Find Global Versions operation
|
|
136
|
+
const result = await sdk.findGlobalVersions({
|
|
137
|
+
slug: 'global',
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
// Find Global Version by ID operation
|
|
141
|
+
const result = await sdk.findGlobalVersionByID({ id: version.id, slug: 'global' })
|
|
142
|
+
|
|
143
|
+
// Restore Global Version operation
|
|
144
|
+
const result = await sdk.restoreGlobalVersion({
|
|
145
|
+
slug: 'global',
|
|
146
|
+
id,
|
|
147
|
+
})
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Every operation has optional 3rd parameter which is used to add additional data to the RequestInit object (like headers):
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
await sdk.me(
|
|
154
|
+
{
|
|
155
|
+
collection: 'users',
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
// RequestInit object
|
|
159
|
+
headers: {
|
|
160
|
+
Authorization: `JWT ${token}`,
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
)
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
To query custom endpoints, you can use the `request` method, which is used internally for all other methods:
|
|
167
|
+
|
|
168
|
+
```ts
|
|
169
|
+
await sdk.request({
|
|
170
|
+
method: 'POST',
|
|
171
|
+
path: '/send-data',
|
|
172
|
+
json: {
|
|
173
|
+
id: 1,
|
|
174
|
+
},
|
|
175
|
+
})
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
Custom `fetch` implementation and `baseInit` for shared `RequestInit` properties:
|
|
179
|
+
|
|
180
|
+
```ts
|
|
181
|
+
const sdk = new PayloadSDK<Config>({
|
|
182
|
+
baseInit: { credentials: 'include' },
|
|
183
|
+
baseURL: 'https://example.com/api',
|
|
184
|
+
fetch: async (url, init) => {
|
|
185
|
+
console.log('before req')
|
|
186
|
+
const response = await fetch(url, init)
|
|
187
|
+
console.log('after req')
|
|
188
|
+
return response
|
|
189
|
+
},
|
|
190
|
+
})
|
|
191
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@payloadcms/sdk",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.58.0",
|
|
4
4
|
"description": "The official Payload REST API SDK",
|
|
5
5
|
"homepage": "https://payloadcms.com",
|
|
6
6
|
"repository": {
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"qs-esm": "7.0.2",
|
|
35
35
|
"ts-essentials": "10.0.3",
|
|
36
|
-
"payload": "3.
|
|
36
|
+
"payload": "3.58.0"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@payloadcms/eslint-config": "3.28.0"
|