chore: migrates to PATCH for collection updates

This commit is contained in:
James
2022-08-15 19:19:44 -07:00
parent 1d8bcd6e16
commit ccada2e8c9
10 changed files with 23 additions and 9 deletions

View File

@@ -34,6 +34,20 @@ export const requests = {
return fetch(url, formattedOptions);
},
patch: (url: string, options: RequestInit = { headers: {} }): Promise<Response> => {
const headers = options && options.headers ? { ...options.headers } : {};
const formattedOptions = {
...options,
method: 'PATCH',
headers: {
...headers,
},
};
return fetch(url, formattedOptions);
},
delete: (url: string, options: RequestInit = { headers: {} }): Promise<Response> => {
const headers = options && options.headers ? { ...options.headers } : {};
return fetch(url, {

View File

@@ -77,7 +77,7 @@ const Autosave: React.FC<Props> = ({ collection, global, id, publishedDocUpdated
if (collection && id) {
url = `${serverURL}${api}/${collection.slug}/${id}?draft=true&autosave=true&locale=${locale}`;
method = 'PUT';
method = 'PATCH';
}
if (global) {

View File

@@ -25,7 +25,7 @@ const SaveDraft: React.FC = () => {
if (collection) {
action = `${serverURL}${api}/${collection.slug}${id ? `/${id}` : ''}${search}`;
if (id) method = 'PUT';
if (id) method = 'PATCH';
}
if (global) {

View File

@@ -55,7 +55,7 @@ const Status: React.FC<Props> = () => {
if (collection) {
url = `${serverURL}${api}/${collection.slug}/${id}?depth=0&locale=${locale}&fallback-locale=null`;
method = 'put';
method = 'PATCH';
}
if (global) {
url = `${serverURL}${api}/globals/${global.slug}?depth=0&locale=${locale}&fallback-locale=null`;

View File

@@ -27,7 +27,7 @@ export type Preferences = {
export type Props = {
disabled?: boolean
onSubmit?: (fields: Fields, data: Data) => void
method?: 'get' | 'put' | 'delete' | 'post'
method?: 'get' | 'patch' | 'delete' | 'post'
action?: string
handleResponse?: (res: Response) => void
onSuccess?: (json: unknown) => void

View File

@@ -61,7 +61,7 @@ const DefaultAccount: React.FC<Props> = (props) => {
<OperationContext.Provider value="update">
<Form
className={`${baseClass}__form`}
method="put"
method="patch"
action={action}
initialState={initialState}
disabled={!hasSavePermission}

View File

@@ -81,7 +81,7 @@ const DefaultEditView: React.FC<Props> = (props) => {
<OperationContext.Provider value={operation}>
<Form
className={`${baseClass}__form`}
method={id ? 'put' : 'post'}
method={id ? 'patch' : 'post'}
action={action}
onSuccess={onSave}
disabled={!hasSavePermission}

View File

@@ -10,7 +10,7 @@ export type UpdateResult = {
};
export async function deprecatedUpdate(req: PayloadRequest, res: Response, next: NextFunction): Promise<Response<UpdateResult> | void> {
console.warn('The PUT method is deprecated and will no longer be supported in a future release. Please use the PATCH method for update requests.');
req.payload.logger.warn('The PUT method is deprecated and will no longer be supported in a future release. Please use the PATCH method for update requests.');
return updateHandler(req, res, next);
}

View File

@@ -4,7 +4,7 @@ import { SanitizedConfig } from '../../config/types';
export default (config: SanitizedConfig) => (
(req: Request, res: Response, next: NextFunction) => {
if (config.cors) {
res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
res.header('Access-Control-Allow-Methods', 'PUT, PATCH, POST, GET, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization, Content-Encoding');
if (config.cors === '*') {

View File

@@ -174,7 +174,7 @@ export class RESTClient {
const response = await fetch(`${this.serverURL}/api/${slug || this.defaultSlug}/${id}${formattedQs}`, {
body: JSON.stringify(data),
headers,
method: 'put',
method: 'PATCH',
});
const { status } = response;
const json = await response.json();