Added RAW data option for assets, entries, content types

This commit is contained in:
Rupenieks 2020-08-28 15:27:12 +02:00
parent 3a61ad5997
commit 198cd6258c
5 changed files with 153 additions and 13 deletions

View file

@ -95,6 +95,23 @@ export const fields = [
default: 100,
description: 'How many results to return.',
},
{
displayName: 'Asset ID',
name: 'assetId',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
resource: [
resource.value
],
operation: [
'get',
],
},
},
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
@ -178,20 +195,30 @@ export const fields = [
],
},
{
displayName: 'Asset ID',
name: 'assetId',
type: 'string',
default: '',
required: true,
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Select Option',
default: {},
displayOptions: {
show: {
resource: [
resource.value
resource.value,
],
operation: [
'get',
'getAll',
'get'
],
},
},
options: [
{
displayName: 'RAW Data',
name: 'rawData',
type: 'boolean',
default: false,
description: 'If the data should be returned RAW instead of parsed.',
},
],
},
] as INodeProperties[];

View file

@ -66,4 +66,30 @@ export const fields = [
},
},
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Select Option',
default: {},
displayOptions: {
show: {
resource: [
resource.value,
],
operation: [
'get',
],
},
},
options: [
{
displayName: 'RAW Data',
name: 'rawData',
type: 'boolean',
default: false,
description: 'If the data should be returned RAW instead of parsed.',
},
],
},
] as INodeProperties[];

View file

@ -118,7 +118,13 @@ export class Contentful implements INodeType {
const id = this.getNodeParameter('contentTypeId', 0) as string;
const options = this.getNodeParameter('options', i) as IDataObject;
responseData = await contentfulApiRequest.call(this, 'GET', `/spaces/${credentials?.spaceId}/environments/${env}/content_types/${id}`);
if (!options.rawData) {
responseData = responseData.fields;
}
}
}
if (resource === 'entry') {
@ -131,8 +137,14 @@ export class Contentful implements INodeType {
const id = this.getNodeParameter('entryId', 0) as string;
const options = this.getNodeParameter('options', i) as IDataObject;
responseData = await contentfulApiRequest.call(this, 'GET', `/spaces/${credentials?.spaceId}/environments/${env}/entries/${id}`, {}, qs);
if (!options.rawData) {
responseData = responseData.fields;
}
} else if (operation === 'getAll') {
const credentials = this.getCredentials('contentfulApi');
@ -142,6 +154,8 @@ export class Contentful implements INodeType {
const env = this.getNodeParameter('environmentId', i) as string;
const options = this.getNodeParameter('options', i) as IDataObject;
Object.assign(qs, additionalFields);
if (qs.equal) {
@ -170,11 +184,29 @@ export class Contentful implements INodeType {
if (returnAll) {
responseData = await contenfulApiRequestAllItems.call(this, 'items', 'GET', `/spaces/${credentials?.spaceId}/environments/${env}/entries`, {}, qs);
if (!options.rawData) {
const assets : IDataObject[] = [];
// tslint:disable-next-line: no-any
responseData.map((asset : any) => {
assets.push(asset.fields);
});
responseData = assets;
}
} else {
const limit = this.getNodeParameter('limit', 0) as number;
qs.limit = limit;
responseData = await contentfulApiRequest.call(this, 'GET', `/spaces/${credentials?.spaceId}/environments/${env}/entries`, {}, qs);
responseData = responseData.items;
if (!options.rawData) {
const assets : IDataObject[] = [];
// tslint:disable-next-line: no-any
responseData.map((asset : any) => {
assets.push(asset.fields);
});
responseData = assets;
}
}
}
}
@ -187,8 +219,14 @@ export class Contentful implements INodeType {
const id = this.getNodeParameter('assetId', 0) as string;
const options = this.getNodeParameter('options', i) as IDataObject;
responseData = await contentfulApiRequest.call(this, 'GET', `/spaces/${credentials?.spaceId}/environments/${env}/assets/${id}`, {}, qs);
if (!options.rawData) {
responseData = responseData.fields;
}
} else if (operation === 'getAll') {
const credentials = this.getCredentials('contentfulApi');
@ -199,6 +237,8 @@ export class Contentful implements INodeType {
const env = this.getNodeParameter('environmentId', i) as string;
const options = this.getNodeParameter('options', i) as IDataObject;
Object.assign(qs, additionalFields);
if (qs.equal) {
@ -227,11 +267,29 @@ export class Contentful implements INodeType {
if (returnAll) {
responseData = await contenfulApiRequestAllItems.call(this, 'items', 'GET', `/spaces/${credentials?.spaceId}/environments/${env}/assets`, {}, qs);
if (!options.rawData) {
const assets : IDataObject[] = [];
// tslint:disable-next-line: no-any
responseData.map((asset : any) => {
assets.push(asset.fields);
});
responseData = assets;
}
} else {
const limit = this.getNodeParameter('limit', 0) as number;
qs.limit = limit;
responseData = await contentfulApiRequest.call(this, 'GET', `/spaces/${credentials?.spaceId}/environments/${env}/assets`, {}, qs);
responseData = responseData.items;
if (!options.rawData) {
const assets : IDataObject[] = [];
// tslint:disable-next-line: no-any
responseData.map((asset : any) => {
assets.push(asset.fields);
});
responseData = assets;
}
}
}
}
@ -247,11 +305,13 @@ export class Contentful implements INodeType {
if (returnAll) {
responseData = await contenfulApiRequestAllItems.call(this, 'items', 'GET', `/spaces/${credentials?.spaceId}/environments/${env}/locales`, {}, qs);
} else {
const limit = this.getNodeParameter('limit', 0) as number;
qs.limit = limit;
responseData = await contentfulApiRequest.call(this, 'GET', `/spaces/${credentials?.spaceId}/environments/${env}/locales`, {}, qs);
responseData = responseData.items;
}
}
}

View file

@ -201,4 +201,31 @@ export const fields = [
},
},
}
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Select Option',
default: {},
displayOptions: {
show: {
resource: [
resource.value,
],
operation: [
'get',
'getAll',
],
},
},
options: [
{
displayName: 'RAW Data',
name: 'rawData',
type: 'boolean',
default: false,
description: 'If the data should be returned RAW instead of parsed.',
},
],
},
] as INodeProperties[];

View file

@ -42,12 +42,12 @@ export async function contentfulApiRequest(this: IExecuteFunctions | IExecuteSin
let errorMessage = error;
if (error.response && error.response.body && error.response.body.details) {
const details = error.response.body.details;
errorMessage = details.errors.map((e: IDataObject) => e.details).join('|');
} else if (error.response && error.response.body && error.response.body.message) {
errorMessage = error.response.body.message;
}
// if (error.response && error.response.body && error.response.body.details) {
// const details = error.response.body.details;
// errorMessage = details.errors.map((e: IDataObject) => e.details).join('|');
// } else if (error.response && error.response.body && error.response.body.message) {
// errorMessage = error.response.body.message;
// }
throw new Error(`Contentful error response [${error.statusCode}]: ${errorMessage}`);
}