Skip to content

Commit

Permalink
v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoFaul committed Jan 18, 2021
1 parent 3c112e0 commit 0d88fe5
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 28 deletions.
70 changes: 68 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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: '<https://my-store.com>',
apiAccessToken: '<api-token>',
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
<page-query>
query Product ($id: ID!) {
product (id: $id) {
id
translated {
name
description
}
}
}
</page-query>
```

Now this product will be available at `this.$page.product`:
```vue
<template>
<Layout>
<h2>{{ $page.product.translated.name }}</h2>
<div v-html="$page.product.translated.description" />
</Layout>
</template>
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default {
input: ['src/index.js', 'src/client.js'],
external: ['axios'],
external: ['axios', 'lodash'],
output: {
dir: 'lib',
format: 'cjs'
Expand Down
11 changes: 2 additions & 9 deletions src/client.js
Original file line number Diff line number Diff line change
@@ -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;
}
41 changes: 31 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,67 @@
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);

api.loadSource(this.loadSource.bind(this));
}

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) {
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 0d88fe5

Please sign in to comment.