From 0d88fe5f5031bb4351e16a2a87aafec210890734 Mon Sep 17 00:00:00 2001 From: marcofaul Date: Mon, 18 Jan 2021 16:39:31 +0100 Subject: [PATCH] v0.1.0 --- README.md | 70 ++++++++++++++++++++++++++++++++++++++++++++++-- package.json | 2 +- rollup.config.js | 2 +- src/client.js | 11 ++------ src/index.js | 41 +++++++++++++++++++++------- yarn.lock | 5 ---- 6 files changed, 103 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 63dec8c..cc6515d 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,13 @@ # gridsome-source-shopware6 -!!! WORK IN PROGRESS !!! - > Shopware 6 source plugin for Gridsome This plugin supports the Store API's. +1. [Install](#install) +2. [Usage](#usage) +3. [Routes & Templates](#templates) +4. [Page Query](#page-query) ## Install @@ -18,3 +20,67 @@ npm: ```bash npm install gridsome-source-shopware6 ``` + +## Usage + +`gridsome.config.js` +```js +module.exports = { + plugins: [ + { + use: 'gridsome-source-shopware6', + options: { + debug: false, + storeUrl: '', + apiAccessToken: '', + types: ['product', 'currency', 'shipping-method', 'category'], // Optional, default is all types + apiVersion: 'v3', + timeout: 60000 + } + } + ] +} +``` + + +## Templates + +Now you can create a template called `ShopwareProduct.vue`, and specify the route for it - Gridsome will automatically generate pages for all products. + +`gridsome.config.js` +```js +module.exports = { + templates: { + product: '/product/:handle' + } +} +``` + + +## Page Query + +Once you have specified the route for a type, you can query it by ID. + +```vue + +query Product ($id: ID!) { + product (id: $id) { + id + translated { + name + description + } + } +} + +``` + +Now this product will be available at `this.$page.product`: +```vue + +``` diff --git a/package.json b/package.json index ac709b8..437649b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "gridsome-source-shopware6", "description": "Shopware 6 source plugin for Gridsome", - "version": "0.0.1", + "version": "0.1.0", "main": "lib/index.js", "engines": { "node": ">=10.x" diff --git a/rollup.config.js b/rollup.config.js index deda218..3e7a3a1 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,6 +1,6 @@ export default { input: ['src/index.js', 'src/client.js'], - external: ['axios'], + external: ['axios', 'lodash'], output: { dir: 'lib', format: 'cjs' diff --git a/src/client.js b/src/client.js index 01b6fe5..ef6b229 100644 --- a/src/client.js +++ b/src/client.js @@ -1,17 +1,10 @@ const axios = require('axios'); -export const createClient = apiAccessToken => { +export const createClient = (apiAccessToken, timeout) => { return axios.create({ + timeout, headers: { "sw-access-key": apiAccessToken } }); } - -export const get = (client, url) => { - const result = client.get(url); - - //@TODO: check status code etc -console.log(result); - return result; -} diff --git a/src/index.js b/src/index.js index 2618aa9..9ae0b17 100644 --- a/src/index.js +++ b/src/index.js @@ -1,24 +1,30 @@ const pluginName = require("./../package.json").name; -import { createClient, get} from './client'; +import {createClient} from './client'; + +const {camelCase} = require('lodash'); class ShopwareSource { - static defaultOptions () { + static defaultOptions() { return { debug: false, storeUrl: '', apiAccessToken: '', - types: [], + types: ['product', 'currency', 'shipping-method', 'category'], apiVersion: 'v3', - perPage: 100, timeout: 60000 } } - constructor (api, options) { + constructor(api, options) { this.api = api; this.options = options; + if (!options.storeUrl ) throw new Error('Missing store url.') + if (!options.apiAccessToken) throw new Error('Missing API access token.') + if (!options.apiVersion) throw new Error('Missing API Version. E.g. v3') + + // merge options this.options = Object.assign(ShopwareSource.defaultOptions(), options); @@ -26,21 +32,36 @@ class ShopwareSource { } async loadSource(actions) { - const client = createClient(this.options.apiAccessToken); + const client = createClient(this.options.apiAccessToken, this.options.timeout); for (const typeName of this.options.types) { let url = `${this.options.storeUrl}/store-api/${this.options.apiVersion}/${typeName.toLowerCase()}`; - const result = await client.get(url); - let data = result.data; + const result = await client.post(url); + + if (result.statusText !== 'OK') { + this.log(result); + continue; + } + + let data = []; + + if (!result) { + this.log(result); + continue; + } else if (result.data && result.data.elements) { + data = result.data.elements; + } else { + data = result.data; + } - this.collectionData(data.elements, actions, typeName); + this.collectionData(data, actions, camelCase(typeName)); } } collectionData(data, actions, typeName) { - const collection = actions.addCollection(typeName); + const collection = actions.addCollection(typeName.replace('-', '')); try { for (let item of data) { diff --git a/yarn.lock b/yarn.lock index eba8663..ee9e354 100644 --- a/yarn.lock +++ b/yarn.lock @@ -245,11 +245,6 @@ callsites@^3.0.0: resolved "https://registry.yarnpkg.com/callsites/-/callsites-3.1.0.tgz#b3630abd8943432f54b3f0519238e33cd7df2f73" integrity sha512-P8BjAsXvZS+VIDUI11hHCQEv74YT67YUi5JJFNWIqL235sBmjX4+qx9Muvls5ivyNENctx46xQLQ3aTuE7ssaQ== -camelcase@^6.2.0: - version "6.2.0" - resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-6.2.0.tgz#924af881c9d525ac9d87f40d964e5cea982a1809" - integrity sha512-c7wVvbw3f37nuobQNtgsgG9POC9qMbNuMQmTCqZv23b6MIz0fcYpBiOlv9gEN/hdLdnZTDQhg6e9Dq5M1vKvfg== - chalk@^2.0.0: version "2.4.2" resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.2.tgz#cd42541677a54333cf541a49108c1432b44c9424"