Skip to content

Commit

Permalink
feat: add findVersions findVersionByID restoreVersion `findGlob…
Browse files Browse the repository at this point in the history
…alVersrions` `findGlobalVersionByID` `restoreGlobalVersion` operations
  • Loading branch information
r1tsuu committed Dec 2, 2024
1 parent 86da15a commit 103dfe1
Show file tree
Hide file tree
Showing 11 changed files with 459 additions and 1 deletion.
29 changes: 29 additions & 0 deletions docs/rest-api/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,35 @@ 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
})
```


Expand Down
58 changes: 58 additions & 0 deletions packages/sdk/src/collections/findVersionByID.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import type { ApplyDisableErrors, SelectType, TypeWithVersion } from 'payload'

import type { PayloadSDK } from '../index.js'
import type {
CollectionSlug,
DataFromCollectionSlug,
PayloadGeneratedTypes,
PopulateType,
TypedLocale,
} from '../types.js'

export type FindVersionByIDOptions<
T extends PayloadGeneratedTypes,
TSlug extends CollectionSlug<T>,
TDisableErrors extends boolean,
> = {
collection: TSlug
depth?: number
disableErrors?: TDisableErrors
draft?: boolean
fallbackLocale?: false | TypedLocale<T>
id: number | string
locale?: 'all' | TypedLocale<T>
populate?: PopulateType<T>
select?: SelectType
}

export async function findVersionByID<
T extends PayloadGeneratedTypes,
TSlug extends CollectionSlug<T>,
TDisableErrors extends boolean,
>(
sdk: PayloadSDK<T>,
options: FindVersionByIDOptions<T, TSlug, TDisableErrors>,
init?: RequestInit,
): Promise<ApplyDisableErrors<TypeWithVersion<DataFromCollectionSlug<T, TSlug>>, TDisableErrors>> {
try {
const response = await sdk.request({
args: options,
init,
method: 'GET',
path: `/${options.collection}/versions/${options.id}`,
})

if (response.ok) {
return response.json()
} else {
throw new Error()
}
} catch {
if (options.disableErrors) {
// @ts-expect-error generic nullable
return null
}

throw new Error(`Error retrieving the version document ${options.collection}/${options.id}`)
}
}
45 changes: 45 additions & 0 deletions packages/sdk/src/collections/findVersions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { PaginatedDocs, SelectType, Sort, TypeWithVersion, Where } from 'payload'

import type { PayloadSDK } from '../index.js'
import type {
CollectionSlug,
DataFromCollectionSlug,
PayloadGeneratedTypes,
PopulateType,
TypedLocale,
} from '../types.js'

export type FindVersionsOptions<
T extends PayloadGeneratedTypes,
TSlug extends CollectionSlug<T>,
> = {
collection: TSlug
depth?: number
draft?: boolean
fallbackLocale?: false | TypedLocale<T>
limit?: number
locale?: 'all' | TypedLocale<T>
page?: number
populate?: PopulateType<T>
select?: SelectType
sort?: Sort
where?: Where
}

export async function findVersions<
T extends PayloadGeneratedTypes,
TSlug extends CollectionSlug<T>,
>(
sdk: PayloadSDK<T>,
options: FindVersionsOptions<T, TSlug>,
init?: RequestInit,
): Promise<PaginatedDocs<TypeWithVersion<DataFromCollectionSlug<T, TSlug>>>> {
const response = await sdk.request({
args: options,
init,
method: 'GET',
path: `/${options.collection}/versions`,
})

return response.json()
}
39 changes: 39 additions & 0 deletions packages/sdk/src/collections/restoreVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { PayloadSDK } from '../index.js'
import type {
CollectionSlug,
DataFromCollectionSlug,
PayloadGeneratedTypes,
PopulateType,
TypedLocale,
} from '../types.js'

export type RestoreVersionByIDOptions<
T extends PayloadGeneratedTypes,
TSlug extends CollectionSlug<T>,
> = {
collection: TSlug
depth?: number
draft?: boolean
fallbackLocale?: false | TypedLocale<T>
id: number | string
locale?: 'all' | TypedLocale<T>
populate?: PopulateType<T>
}

export async function restoreVersion<
T extends PayloadGeneratedTypes,
TSlug extends CollectionSlug<T>,
>(
sdk: PayloadSDK<T>,
options: RestoreVersionByIDOptions<T, TSlug>,
init?: RequestInit,
): Promise<DataFromCollectionSlug<T, TSlug>> {
const response = await sdk.request({
args: options,
init,
method: 'POST',
path: `/${options.collection}/versions/${options.id}`,
})

return response.json()
}
58 changes: 58 additions & 0 deletions packages/sdk/src/globals/findVersionByID.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import type { ApplyDisableErrors, SelectType, TypeWithVersion } from 'payload'

import type { PayloadSDK } from '../index.js'
import type {
DataFromGlobalSlug,
GlobalSlug,
PayloadGeneratedTypes,
PopulateType,
TypedLocale,
} from '../types.js'

export type FindGlobalVersionByIDOptions<
T extends PayloadGeneratedTypes,
TSlug extends GlobalSlug<T>,
TDisableErrors extends boolean,
> = {
depth?: number
disableErrors?: TDisableErrors
draft?: boolean
fallbackLocale?: false | TypedLocale<T>
id: number | string
locale?: 'all' | TypedLocale<T>
populate?: PopulateType<T>
select?: SelectType
slug: TSlug
}

export async function findGlobalVersionByID<
T extends PayloadGeneratedTypes,
TSlug extends GlobalSlug<T>,
TDisableErrors extends boolean,
>(
sdk: PayloadSDK<T>,
options: FindGlobalVersionByIDOptions<T, TSlug, TDisableErrors>,
init?: RequestInit,
): Promise<ApplyDisableErrors<TypeWithVersion<DataFromGlobalSlug<T, TSlug>>, TDisableErrors>> {
try {
const response = await sdk.request({
args: options,
init,
method: 'GET',
path: `/globals/${options.slug}/versions/${options.id}`,
})

if (response.ok) {
return response.json()
} else {
throw new Error()
}
} catch {
if (options.disableErrors) {
// @ts-expect-error generic nullable
return null
}

throw new Error(`Error retrieving the version document ${options.slug}/${options.id}`)
}
}
45 changes: 45 additions & 0 deletions packages/sdk/src/globals/findVersions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { PaginatedDocs, SelectType, Sort, TypeWithVersion, Where } from 'payload'

import type { PayloadSDK } from '../index.js'
import type {
DataFromGlobalSlug,
GlobalSlug,
PayloadGeneratedTypes,
PopulateType,
TypedLocale,
} from '../types.js'

export type FindGlobalVersionsOptions<
T extends PayloadGeneratedTypes,
TSlug extends GlobalSlug<T>,
> = {
depth?: number
draft?: boolean
fallbackLocale?: false | TypedLocale<T>
limit?: number
locale?: 'all' | TypedLocale<T>
page?: number
populate?: PopulateType<T>
select?: SelectType
slug: TSlug
sort?: Sort
where?: Where
}

export async function findGlobalVersions<
T extends PayloadGeneratedTypes,
TSlug extends GlobalSlug<T>,
>(
sdk: PayloadSDK<T>,
options: FindGlobalVersionsOptions<T, TSlug>,
init?: RequestInit,
): Promise<PaginatedDocs<TypeWithVersion<DataFromGlobalSlug<T, TSlug>>>> {
const response = await sdk.request({
args: options,
init,
method: 'GET',
path: `/globals/${options.slug}/versions`,
})

return response.json()
}
43 changes: 43 additions & 0 deletions packages/sdk/src/globals/restoreVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import type { TypeWithVersion } from 'payload'

import type { PayloadSDK } from '../index.js'
import type {
DataFromGlobalSlug,
GlobalSlug,
PayloadGeneratedTypes,
PopulateType,
TypedLocale,
} from '../types.js'

export type RestoreGlobalVersionByIDOptions<
T extends PayloadGeneratedTypes,
TSlug extends GlobalSlug<T>,
> = {
depth?: number
draft?: boolean
fallbackLocale?: false | TypedLocale<T>
id: number | string
locale?: 'all' | TypedLocale<T>
populate?: PopulateType<T>
slug: TSlug
}

export async function restoreGlobalVersion<
T extends PayloadGeneratedTypes,
TSlug extends GlobalSlug<T>,
>(
sdk: PayloadSDK<T>,
options: RestoreGlobalVersionByIDOptions<T, TSlug>,
init?: RequestInit,
): Promise<TypeWithVersion<DataFromGlobalSlug<T, TSlug>>> {
const response = await sdk.request({
args: options,
init,
method: 'POST',
path: `/globals/${options.slug}/versions/${options.id}`,
})

const { doc } = await response.json()

return doc
}
Loading

0 comments on commit 103dfe1

Please sign in to comment.