Files
payloadcms/test/package.json
Sasha 92a5f075b6 feat: add Payload SDK package (#9463)
Adds Payload SDK package, which can be used to query 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, can't even notice the
difference:
Example:
```ts
import { PayloadSDK } from '@payloadcms/sdk'
import type { Config } from './payload-types'

// Pass your config from generated types as generic
const sdk = new PayloadSDK<Config>({
  baseURL: 'https://example.com/api',
})

// Find operation
const posts = await sdk.find({
  collection: 'posts',
  draft: true,
  limit: 10,
  locale: 'en',
  page: 1,
  where: { _status: { equals: 'published' } },
})

// Find by ID operation
const posts = await sdk.findByID({
  id,
  collection: 'posts',
  draft: true,
  locale: 'en',
})

// Auth login operation
const result = await sdk.login({
  collection: 'users',
  data: {
    email: 'dev@payloadcms.com',
    password: '12345',
  },
})

// Create operation
const result = await sdk.create({
  collection: 'posts',
  data: { text: 'text' },
})

// Create operation with a file
// `file` can be either a Blob | File object or a string URL
const result = await sdk.create({ collection: 'media', file, data: {} })

// Count operation
const result = await sdk.count({ collection: 'posts', where: { id: { equals: post.id } } })

// Update (by ID) operation
const result = await sdk.update({
  collection: 'posts',
  id: post.id,
  data: {
    text: 'updated-text',
  },
})

// Update (bulk) operation
const result = await sdk.update({
  collection: 'posts',
  where: {
    id: {
      equals: post.id,
    },
  },
  data: { text: 'updated-text-bulk' },
})

// Delete (by ID) operation
const result = await sdk.delete({ id: post.id, collection: 'posts' })

// Delete (bulk) operation
const result = await sdk.delete({ where: { id: { equals: post.id } }, collection: 'posts' })

// Find Global operation
const result = await sdk.findGlobal({ slug: 'global' })

// Update Global operation
const result = await sdk.updateGlobal({ slug: 'global', data: { text: 'some-updated-global' } })

// Auth Login operation
const result = await sdk.login({
  collection: 'users',
  data: { email: 'dev@payloadcms.com', password: '123456' },
})

// Auth Me operation
const result = await sdk.me(
  { collection: 'users' },
  {
    headers: {
      Authorization: `JWT  ${user.token}`,
    },
  },
)

// Auth Refresh Token operation
const result = await sdk.refreshToken(
  { collection: 'users' },
  { headers: { Authorization: `JWT ${user.token}` } },
)

// Auth Forgot Password operation
const result = await sdk.forgotPassword({
  collection: 'users',
  data: { email: user.email },
})

// Auth Reset Password operation
const result = await sdk.resetPassword({
  collection: 'users',
  data: { password: '1234567', token: resetPasswordToken },
})

// Find Versions operation
const result = await sdk.findVersions({
  collection: 'posts',
  where: { parent: { equals: post.id } },
})

// Find Version by ID operation
const result = await sdk.findVersionByID({ collection: 'posts', id: version.id })

// Restore Version operation
const result = await sdk.restoreVersion({
  collection: 'posts',
  id,
})

// Find Global Versions operation
const result = await sdk.findGlobalVersions({
  slug: 'global',
})

// Find Global Version by ID operation
const result = await sdk.findGlobalVersionByID({ id: version.id, slug: 'global' })

// Restore Global Version operation
const result = await sdk.restoreGlobalVersion({
  slug: 'global',
  id
})
```



Every operation has optional 3rd parameter which is used to add
additional data to the RequestInit object (like headers):
```ts
await sdk.me({
  collection: "users"
}, {
  // RequestInit object
  headers: {
    Authorization: `JWT ${token}`
  }
})
``` 

To query custom endpoints, you can use the `request` method, which is
used internally for all other methods:
```ts
await sdk.request({
  method: 'POST',
  path: '/send-data',
  json: {
    id: 1,
  },
})
```

Custom `fetch` implementation and `baseInit` for shared `RequestInit`
properties:
```ts
const sdk = new PayloadSDK<Config>({
  baseInit: { credentials: 'include' },
  baseURL: 'https://example.com/api',
  fetch: async (url, init) => {
    console.log('before req')
    const response = await fetch(url, init)
    console.log('after req')
    return response
  },
})
```
2025-09-29 17:01:01 -04:00

110 lines
3.8 KiB
JSON

{
"name": "payload-test-suite",
"version": "0.0.1",
"private": true,
"description": "Payload test suite",
"type": "module",
"scripts": {
"dev": "cross-env NODE_OPTIONS=--no-deprecation node ./dev.js",
"test": "pnpm -C \"../\" run test",
"test:e2e": "pnpm -C \"../\" run test:e2e",
"test:int": "pnpm -C \"../\" run test:int",
"typecheck": "pnpm turbo build --filter payload-test-suite && tsc --project tsconfig.typecheck.json"
},
"lint-staged": {
"**/package.json": "sort-package-json",
"*.{md,mdx,yml,json}": "prettier --write",
"*.{js,jsx,ts,tsx}": [
"prettier --write",
"eslint --cache --fix"
],
"templates/website/**/*": "sh -c \"cd templates/website; pnpm install --ignore-workspace --frozen-lockfile; pnpm run lint --fix\"",
"tsconfig.json": "node scripts/reset-tsconfig.js"
},
"devDependencies": {
"@aws-sdk/client-s3": "^3.614.0",
"@azure/storage-blob": "^12.11.0",
"@date-fns/tz": "1.2.0",
"@miniflare/d1": "2.14.4",
"@miniflare/shared": "2.14.4",
"@next/env": "15.4.4",
"@payloadcms/admin-bar": "workspace:*",
"@payloadcms/db-d1-sqlite": "workspace:*",
"@payloadcms/db-mongodb": "workspace:*",
"@payloadcms/db-postgres": "workspace:*",
"@payloadcms/db-sqlite": "workspace:*",
"@payloadcms/db-vercel-postgres": "workspace:*",
"@payloadcms/drizzle": "workspace:*",
"@payloadcms/email-nodemailer": "workspace:*",
"@payloadcms/email-resend": "workspace:*",
"@payloadcms/eslint-config": "workspace:*",
"@payloadcms/eslint-plugin": "workspace:*",
"@payloadcms/graphql": "workspace:*",
"@payloadcms/live-preview": "workspace:*",
"@payloadcms/live-preview-react": "workspace:*",
"@payloadcms/next": "workspace:*",
"@payloadcms/payload-cloud": "workspace:*",
"@payloadcms/plugin-cloud-storage": "workspace:*",
"@payloadcms/plugin-form-builder": "workspace:*",
"@payloadcms/plugin-import-export": "workspace:*",
"@payloadcms/plugin-multi-tenant": "workspace:*",
"@payloadcms/plugin-nested-docs": "workspace:*",
"@payloadcms/plugin-redirects": "workspace:*",
"@payloadcms/plugin-search": "workspace:*",
"@payloadcms/plugin-sentry": "workspace:*",
"@payloadcms/plugin-seo": "workspace:*",
"@payloadcms/plugin-stripe": "workspace:*",
"@payloadcms/richtext-lexical": "workspace:*",
"@payloadcms/richtext-slate": "workspace:*",
"@payloadcms/sdk": "workspace:*",
"@payloadcms/storage-azure": "workspace:*",
"@payloadcms/storage-gcs": "workspace:*",
"@payloadcms/storage-s3": "workspace:*",
"@payloadcms/storage-uploadthing": "workspace:*",
"@payloadcms/storage-vercel-blob": "workspace:*",
"@payloadcms/translations": "workspace:*",
"@payloadcms/ui": "workspace:*",
"@sentry/nextjs": "^8.33.1",
"@sentry/react": "^7.77.0",
"@types/jest": "29.5.12",
"@types/react": "19.1.12",
"@types/react-dom": "19.1.9",
"babel-plugin-react-compiler": "19.1.0-rc.3",
"better-sqlite3": "11.10.0",
"comment-json": "^4.2.3",
"create-payload-app": "workspace:*",
"csv-parse": "^5.6.0",
"dequal": "2.0.3",
"dotenv": "16.4.7",
"drizzle-kit": "0.31.4",
"drizzle-orm": "0.44.2",
"escape-html": "1.0.3",
"eslint-plugin-playwright": "2.2.0",
"execa": "5.1.1",
"file-type": "19.3.0",
"http-status": "2.1.0",
"jest": "29.7.0",
"jwt-decode": "4.0.0",
"mongoose": "8.15.1",
"next": "15.4.4",
"nodemailer": "6.9.16",
"object-to-formdata": "4.5.1",
"payload": "workspace:*",
"pg": "8.16.3",
"qs-esm": "7.0.2",
"react": "19.1.1",
"react-dom": "19.1.1",
"sass": "1.77.4",
"server-only": "^0.0.1",
"sharp": "0.32.6",
"slate": "0.91.4",
"tempy": "^1.0.1",
"ts-essentials": "10.0.3",
"typescript": "5.7.3",
"uuid": "10.0.0"
},
"pnpm": {
"neverBuiltDependencies": []
}
}