-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
findVersions
findVersionByID
restoreVersion
`findGlob…
…alVersrions` `findGlobalVersionByID` `restoreGlobalVersion` operations
- Loading branch information
Showing
11 changed files
with
459 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.