Skip to content
This repository has been archived by the owner on Dec 27, 2022. It is now read-only.

Init migration to v4 #266

Merged
merged 41 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
43d484a
Update dependencies
soupette Jan 21, 2022
35e7f6b
Init migration
soupette Jan 21, 2022
0ff61e2
Fix relations and file download
soupette Jan 21, 2022
73f0dd8
Fix multiple images and add support for components
soupette Jan 21, 2022
d760c9e
Rename index.js to gatsby-node, clean data for dynamiczone
soupette Jan 25, 2022
5373051
Init DZ support
soupette Jan 25, 2022
9fb72a1
Delete old nodes
soupette Jan 25, 2022
8682f9e
Fix content digest
soupette Jan 26, 2022
fb02bbc
Add comments
soupette Jan 26, 2022
d522b70
Fix type naming and delete only parent nodes
soupette Jan 26, 2022
2182fac
Only fetch data after build
soupette Jan 26, 2022
fed9db5
Merge pull request #267 from strapi/delta-fetch
soupette Jan 27, 2022
080fdf2
Extract images for markdown field
soupette Jan 27, 2022
874ca31
Extract markdown images
soupette Jan 27, 2022
dcec77f
Fix PR feedback
soupette Jan 28, 2022
0d557a7
Merge pull request #268 from strapi/extract-markdown-files
soupette Jan 28, 2022
f91f7fd
Refacto code
soupette Jan 28, 2022
d246601
Merge pull request #269 from strapi/refactoring
soupette Jan 28, 2022
e54a281
Create manifest
soupette Feb 7, 2022
69fab3a
Change manifestId
soupette Feb 8, 2022
5d180b2
Merge pull request #274 from strapi/content-sync
soupette Feb 8, 2022
82d3072
Update plugin config and README accordingly
soupette Feb 8, 2022
a5b5595
Merge pull request #275 from strapi/improve-config
soupette Feb 9, 2022
a1cd966
Add support for user content type and file content type
soupette Feb 9, 2022
f2fb197
Fix PR feedback
soupette Feb 9, 2022
e1e6116
Merge pull request #276 from strapi/fix-users-content-type
soupette Feb 9, 2022
bba30b1
v2.0.0-alpha.1
soupette Feb 9, 2022
780acb2
Refacto README
soupette Feb 9, 2022
e1a29e0
Set timestamps after fetching models
soupette Feb 10, 2022
806963f
Update readme and fix restricted field
soupette Feb 10, 2022
6298b76
Fix PR feedback
soupette Feb 10, 2022
aa4f32f
Merge pull request #279 from strapi/update-readme
remidej Feb 10, 2022
73cf9bd
Fix typo
soupette Feb 10, 2022
e4db837
v2.0.0-alpha.2
soupette Feb 10, 2022
4f4298e
v2.0.0-alpha.3
soupette Feb 10, 2022
f897fc9
Update readme
soupette Feb 11, 2022
a2ab208
Fix PR feedback
soupette Feb 11, 2022
1fde793
Merge pull request #282 from strapi/fix-readme
soupette Feb 11, 2022
e74dafd
Fix PR feedback
soupette Feb 15, 2022
3195a48
v2.0.0-alpha.4
soupette Feb 15, 2022
25c0ff1
Fix conflicts
soupette Feb 15, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
425 changes: 268 additions & 157 deletions README.md

Large diffs are not rendered by default.

Binary file added assets/webhook.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./lib')
module.exports = require('./lib/gatsby-node');
18 changes: 12 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gatsby-source-strapi",
"version": "1.0.2",
"version": "2.0.0-alpha.3",
"description": "Gatsby source plugin for building websites using Strapi as a data source",
"author": {
"email": "hi@strapi.io",
Expand Down Expand Up @@ -46,11 +46,11 @@
"strapi"
],
"dependencies": {
"axios": "0.21.1",
"gatsby-node-helpers": "0.3.0",
"gatsby-source-filesystem": "3.7.1",
"lodash": "4.17.21",
"pluralize": "8.0.0"
"axios": "0.24.0",
"commonmark": "^0.30.0",
"gatsby-source-filesystem": "4.5.1",
"lodash": "^4.17.21",
"qs": "^6.10.3"
},
"devDependencies": {
"@babel/cli": "7.14.5",
Expand All @@ -62,6 +62,12 @@
"lint-staged": "10.5.1",
"prettier": "2.1.2"
},
"peerDependencies": {
"gatsby": "^4.0.0",
"gatsby-plugin-image": "^2.5.1",
"gatsby-plugin-sharp": "^4.5.1",
"sharp": "^0.29.0"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
Expand Down
27 changes: 0 additions & 27 deletions src/authentication.js

This file was deleted.

18 changes: 18 additions & 0 deletions src/axiosInstance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import axios from 'axios';

const createInstance = (config) => {
const headers = { ...config?.headers };

if (config.accessToken) {
headers.authorization = `Bearer ${config.accessToken}`;
}

const instance = axios.create({
baseURL: config.apiURL,
headers,
});

return instance;
};

export default createInstance;
163 changes: 163 additions & 0 deletions src/clean-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import _ from 'lodash';

import { getContentTypeSchema } from './helpers';

const MEDIA_FIELDS = [
'name',
'alternativeText',
'caption',
'width',
'height',
'formats',
'hash',
'ext',
'mime',
'size',
'url',
'previewUrl',
'createdAt',
'updatedAt',
];

const restrictedFields = ['__component', `children`, `fields`, `internal`, `parent`];

/**
* Removes the attribute key in the entire data.
* @param {Object} attributes response from the API
* @param {Object} currentSchema
* @param {*} schemas
* @returns
*/
export const cleanAttributes = (attributes, currentSchema, schemas) => {
if (!attributes) {
return null;
}

return Object.entries(attributes).reduce((acc, [name, value]) => {
const attribute = currentSchema.schema.attributes[name];

const attributeName = restrictedFields.includes(name) ? _.snakeCase(`strapi_${name}`) : name;

if (!attribute?.type) {
acc[attributeName] = value;

return acc;
}

if (!value) {
acc[attributeName] = value;

return acc;
}

// Changing the format in order to extract images from the richtext field
// NOTE: We could add an option to disable the extraction
if (attribute.type === 'richtext') {
return {
...acc,
[attributeName]: {
data: value,
medias: [],
},
};
}

if (attribute.type === 'dynamiczone') {
return {
...acc,
[attributeName]: value.map((v) => {
const compoSchema = getContentTypeSchema(schemas, v.__component);

return cleanAttributes(v, compoSchema, schemas);
}),
};
}

if (attribute.type === 'component') {
const isRepeatable = attribute.repeatable;
const compoSchema = getContentTypeSchema(schemas, attribute.component);

if (isRepeatable) {
return {
...acc,
[attributeName]: value.map((v) => {
return cleanAttributes(v, compoSchema, schemas);
}),
};
}

return {
...acc,
[attributeName]: cleanAttributes(value, compoSchema, schemas),
};
}

if (attribute.type === 'media') {
if (Array.isArray(value?.data)) {
return {
...acc,
[attributeName]: value.data
? value.data.map(({ id, attributes }) => ({
id,
..._.pick(attributes, MEDIA_FIELDS),
}))
: null,
};
}

return {
...acc,
[attributeName]: value.data
? {
id: value.data.id,
..._.pick(value.data.attributes, MEDIA_FIELDS),
}
: null,
};
}

if (attribute.type === 'relation') {
const relationSchema = getContentTypeSchema(schemas, attribute.target);

if (Array.isArray(value?.data)) {
return {
...acc,
[attributeName]: value.data.map(({ id, attributes }) =>
cleanAttributes({ id, ...attributes }, relationSchema, schemas)
),
};
}

return {
...acc,
[attributeName]: cleanAttributes(
value.data ? { id: value.data.id, ...value.data.attributes } : null,
relationSchema,
schemas
),
};
}

acc[attributeName] = value;

return acc;
}, {});
};

/**
* Transform v4 API response to the v3 format, remove data and attributes key
* Transform richtext field to prepare media extraction
* @param {Object} data
* @param {Object} ctx
* @returns {Object}
*/
export const cleanData = ({ id, attributes, ...rest }, ctx) => {
const { schemas, contentTypeUid } = ctx;
const currentContentTypeSchema = getContentTypeSchema(schemas, contentTypeUid);

return {
id,
...rest,
...cleanAttributes(attributes, currentContentTypeSchema, schemas),
};
};
Loading