From c15637a0a8feb50856af3e0bc05b0b67a046c379 Mon Sep 17 00:00:00 2001 From: Jan Oberhauser Date: Wed, 11 Nov 2020 09:52:05 +0100 Subject: [PATCH] :zap: Minor improvements to Strapi-Node --- .../nodes-base/nodes/Strapi/GenericFunctions.ts | 9 +++------ packages/nodes-base/nodes/Strapi/Strapi.node.ts | 15 ++++++++------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/packages/nodes-base/nodes/Strapi/GenericFunctions.ts b/packages/nodes-base/nodes/Strapi/GenericFunctions.ts index e6061998e3..fbd7c57ff0 100644 --- a/packages/nodes-base/nodes/Strapi/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Strapi/GenericFunctions.ts @@ -19,9 +19,7 @@ export async function strapiApiRequest(this: IExecuteFunctions | ILoadOptionsFun try { const options: OptionsWithUri = { - headers: { - 'Authorization': `Bearer ${qs.jwt}`, - }, + headers: {}, method, body, qs, @@ -34,7 +32,6 @@ export async function strapiApiRequest(this: IExecuteFunctions | ILoadOptionsFun if (Object.keys(body).length === 0) { delete options.body; } - delete qs.jwt; //@ts-ignore return await this.helpers?.request(options); @@ -74,7 +71,7 @@ export async function getToken(this: IExecuteFunctions | ILoadOptionsFunctions | return this.helpers.request!(options); } -export async function strapiApiRequestAllItems(this: IHookFunctions | ILoadOptionsFunctions | IExecuteFunctions, method: string, resource: string, body: any = {}, query: IDataObject = {}): Promise { // tslint:disable-line:no-any +export async function strapiApiRequestAllItems(this: IHookFunctions | ILoadOptionsFunctions | IExecuteFunctions, method: string, resource: string, body: any = {}, query: IDataObject = {}, headers: IDataObject = {}): Promise { // tslint:disable-line:no-any const returnData: IDataObject[] = []; @@ -85,7 +82,7 @@ export async function strapiApiRequestAllItems(this: IHookFunctions | ILoadOptio query._start = 0; do { - responseData = await strapiApiRequest.call(this, method, resource, body, query); + responseData = await strapiApiRequest.call(this, method, resource, body, query, undefined, headers); query._start += query._limit; returnData.push.apply(returnData, responseData); } while ( diff --git a/packages/nodes-base/nodes/Strapi/Strapi.node.ts b/packages/nodes-base/nodes/Strapi/Strapi.node.ts index 3a973a1f99..74849e6187 100644 --- a/packages/nodes-base/nodes/Strapi/Strapi.node.ts +++ b/packages/nodes-base/nodes/Strapi/Strapi.node.ts @@ -66,13 +66,14 @@ export class Strapi implements INodeType { const returnData: IDataObject[] = []; const length = (items.length as unknown) as number; const qs: IDataObject = {}; + const headers: IDataObject = {}; let responseData; const resource = this.getNodeParameter('resource', 0) as string; const operation = this.getNodeParameter('operation', 0) as string; const { jwt } = await getToken.call(this); - qs.jwt = jwt; + headers.Authorization = `Bearer ${jwt}`; if (resource === 'entry') { if (operation === 'create') { @@ -91,7 +92,7 @@ export class Strapi implements INodeType { body[key] = items[i].json[key]; } } - responseData = await strapiApiRequest.call(this, 'POST', `/${contentType}`, body, qs); + responseData = await strapiApiRequest.call(this, 'POST', `/${contentType}`, body, qs, undefined, headers); returnData.push(responseData); } @@ -103,7 +104,7 @@ export class Strapi implements INodeType { const entryId = this.getNodeParameter('entryId', i) as string; - responseData = await strapiApiRequest.call(this, 'DELETE', `/${contentType}/${entryId}`, {}, qs); + responseData = await strapiApiRequest.call(this, 'DELETE', `/${contentType}/${entryId}`, {}, qs, undefined, headers); returnData.push(responseData); } @@ -137,11 +138,11 @@ export class Strapi implements INodeType { } if (returnAll) { - responseData = await strapiApiRequestAllItems.call(this, 'GET', `/${contentType}`, {}, qs); + responseData = await strapiApiRequestAllItems.call(this, 'GET', `/${contentType}`, {}, qs, headers); } else { qs._limit = this.getNodeParameter('limit', i) as number; - responseData = await strapiApiRequest.call(this, 'GET', `/${contentType}`, {}, qs); + responseData = await strapiApiRequest.call(this, 'GET', `/${contentType}`, {}, qs, undefined, headers); } returnData.push.apply(returnData, responseData); } @@ -154,7 +155,7 @@ export class Strapi implements INodeType { const entryId = this.getNodeParameter('entryId', i) as string; - responseData = await strapiApiRequest.call(this, 'GET', `/${contentType}/${entryId}`, {}, qs); + responseData = await strapiApiRequest.call(this, 'GET', `/${contentType}/${entryId}`, {}, qs, undefined, headers); returnData.push(responseData); } @@ -180,7 +181,7 @@ export class Strapi implements INodeType { body[key] = items[i].json[key]; } } - responseData = await strapiApiRequest.call(this, 'PUT', `/${contentType}/${entryId}`, body, qs); + responseData = await strapiApiRequest.call(this, 'PUT', `/${contentType}/${entryId}`, body, qs, undefined, headers); returnData.push(responseData); }