Minor improvements to Strapi-Node

This commit is contained in:
Jan Oberhauser 2020-11-11 09:52:05 +01:00
parent 73e1399c42
commit c15637a0a8
2 changed files with 11 additions and 13 deletions

View file

@ -19,9 +19,7 @@ export async function strapiApiRequest(this: IExecuteFunctions | ILoadOptionsFun
try { try {
const options: OptionsWithUri = { const options: OptionsWithUri = {
headers: { headers: {},
'Authorization': `Bearer ${qs.jwt}`,
},
method, method,
body, body,
qs, qs,
@ -34,7 +32,6 @@ export async function strapiApiRequest(this: IExecuteFunctions | ILoadOptionsFun
if (Object.keys(body).length === 0) { if (Object.keys(body).length === 0) {
delete options.body; delete options.body;
} }
delete qs.jwt;
//@ts-ignore //@ts-ignore
return await this.helpers?.request(options); return await this.helpers?.request(options);
@ -74,7 +71,7 @@ export async function getToken(this: IExecuteFunctions | ILoadOptionsFunctions |
return this.helpers.request!(options); return this.helpers.request!(options);
} }
export async function strapiApiRequestAllItems(this: IHookFunctions | ILoadOptionsFunctions | IExecuteFunctions, method: string, resource: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any export async function strapiApiRequestAllItems(this: IHookFunctions | ILoadOptionsFunctions | IExecuteFunctions, method: string, resource: string, body: any = {}, query: IDataObject = {}, headers: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const returnData: IDataObject[] = []; const returnData: IDataObject[] = [];
@ -85,7 +82,7 @@ export async function strapiApiRequestAllItems(this: IHookFunctions | ILoadOptio
query._start = 0; query._start = 0;
do { 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; query._start += query._limit;
returnData.push.apply(returnData, responseData); returnData.push.apply(returnData, responseData);
} while ( } while (

View file

@ -66,13 +66,14 @@ export class Strapi implements INodeType {
const returnData: IDataObject[] = []; const returnData: IDataObject[] = [];
const length = (items.length as unknown) as number; const length = (items.length as unknown) as number;
const qs: IDataObject = {}; const qs: IDataObject = {};
const headers: IDataObject = {};
let responseData; let responseData;
const resource = this.getNodeParameter('resource', 0) as string; const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string; const operation = this.getNodeParameter('operation', 0) as string;
const { jwt } = await getToken.call(this); const { jwt } = await getToken.call(this);
qs.jwt = jwt; headers.Authorization = `Bearer ${jwt}`;
if (resource === 'entry') { if (resource === 'entry') {
if (operation === 'create') { if (operation === 'create') {
@ -91,7 +92,7 @@ export class Strapi implements INodeType {
body[key] = items[i].json[key]; 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); returnData.push(responseData);
} }
@ -103,7 +104,7 @@ export class Strapi implements INodeType {
const entryId = this.getNodeParameter('entryId', i) as string; 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); returnData.push(responseData);
} }
@ -137,11 +138,11 @@ export class Strapi implements INodeType {
} }
if (returnAll) { if (returnAll) {
responseData = await strapiApiRequestAllItems.call(this, 'GET', `/${contentType}`, {}, qs); responseData = await strapiApiRequestAllItems.call(this, 'GET', `/${contentType}`, {}, qs, headers);
} else { } else {
qs._limit = this.getNodeParameter('limit', i) as number; 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); returnData.push.apply(returnData, responseData);
} }
@ -154,7 +155,7 @@ export class Strapi implements INodeType {
const entryId = this.getNodeParameter('entryId', i) as string; 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); returnData.push(responseData);
} }
@ -180,7 +181,7 @@ export class Strapi implements INodeType {
body[key] = items[i].json[key]; 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); returnData.push(responseData);
} }