Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.

Commit c128652

Browse files
committed
Inital commit
0 parents  commit c128652

21 files changed

+2674
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

example/product-integration.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {createFromIntegration} from '../src/index.js';
2+
import Criteria from '../src/data/criteria.data.js';
3+
4+
async function test()
5+
{
6+
let api = await createFromIntegration('<url>', 'SWIAD0VBY1HTR2PSTM5OAUVHMQ', 'eXcxZUlDWW5IZG1GRk5iM1MwUnRjb2cwN0dBcjFOQ2lySlUwYXk', 1);
7+
8+
let repository = api.create('product');
9+
let criteria = new Criteria();
10+
criteria.addFilter(Criteria.equals('parentId', null));
11+
12+
let products = await repository.search(criteria, api.defaultContext());
13+
14+
const product = products[0];
15+
console.log(product.name);
16+
product.name = 'Node Test';
17+
console.log(product.name);
18+
19+
await repository.save(products[0], api.defaultContext());
20+
}
21+
22+
test();

example/product-password.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {createFromPasswordAndLogin} from '../src/index.js';
2+
import Criteria from '../src/data/criteria.data.js';
3+
4+
async function test()
5+
{
6+
let api = await createFromPasswordAndLogin('<url>', 'demo', 'demo', 1);
7+
8+
let repository = api.create('product');
9+
let criteria = new Criteria();
10+
criteria.addFilter(Criteria.equals('parentId', null));
11+
12+
let products = await repository.search(criteria, api.defaultContext());
13+
14+
const product = products[0];
15+
console.log(product.name);
16+
product.name = 'Node Test';
17+
console.log(product.name);
18+
19+
await repository.save(products[0], api.defaultContext());
20+
}
21+
22+
test();

package-lock.json

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "shopware-admin-api",
3+
"version": "0.1.0",
4+
"description": "Shopware API",
5+
"main": "index.js",
6+
"type": "module",
7+
"scripts": {
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git+https://github.com/shyim/shopware-admin-api-node.git"
13+
},
14+
"keywords": [
15+
"Shopware",
16+
"Admin-API"
17+
],
18+
"author": "Shyim",
19+
"license": "MIT",
20+
"bugs": {
21+
"url": "https://github.com/shyim/shopware-admin-api-node/issues"
22+
},
23+
"homepage": "https://github.com/shyim/shopware-admin-api-node#readme",
24+
"dependencies": {
25+
"axios": "^0.19.2",
26+
"lodash": "^4.17.15",
27+
"uuid": "^8.1.0"
28+
}
29+
}

src/api.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import {createClient} from './client.js';
2+
import EntityDefinition from './factory/entity-definition.factory.js';
3+
import EntityHydrator from './data/entity-hydrator.data.js';
4+
import ChangesetGenerator from './data/changeset-generator.data.js';
5+
import EntityFactory from './data/entity-factory.data.js';
6+
import Repository from './data/repository.data.js';
7+
8+
export default class Api {
9+
constructor(url, token, version) {
10+
this.url = url;
11+
this.client = createClient(url, token, version)
12+
this.version = version;
13+
}
14+
15+
async _initialize() {
16+
let schema = await this.client.get('_info/entity-schema.json');
17+
18+
this.EntityDefinition = EntityDefinition;
19+
20+
Object.keys(schema.data).forEach((entityName) => {
21+
this.EntityDefinition.add(entityName, schema.data[entityName]);
22+
});
23+
24+
const hydrator = new EntityHydrator(this.EntityDefinition);
25+
const changesetGenerator = new ChangesetGenerator(this.EntityDefinition);
26+
const entityFactory = new EntityFactory();
27+
28+
this.create = (entityName, route, options) => {
29+
if (!route) {
30+
route = `/${entityName.replace(/_/g, '-')}`;
31+
}
32+
options = options || {};
33+
34+
if (options.version === undefined) {
35+
options.version = this.version;
36+
}
37+
38+
const definition = this.EntityDefinition.get(entityName);
39+
40+
return new Repository(
41+
route,
42+
definition.entity,
43+
this.client,
44+
hydrator,
45+
changesetGenerator,
46+
entityFactory,
47+
options
48+
);
49+
};
50+
}
51+
52+
defaultContext() {
53+
return {
54+
apiPath: `${this.url}/api`,
55+
apiResourcePath: `${this.url}/api/v${this.version}`,
56+
apiVersion: this.version,
57+
authToken: {
58+
access: this.client.token.access_token
59+
}
60+
}
61+
}
62+
}

src/client.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import RefreshTokenHelper from './utils/refresh-token.helper.js';
2+
import axios from 'axios';
3+
4+
export function createClient(url, token, version) {
5+
const client = axios.create({
6+
baseURL: `${url}/api/v${version}`
7+
})
8+
9+
client.defaultUrl = url;
10+
client.token = token;
11+
12+
client.defaults.headers.Authorization = `Bearer ${token.access_token}`;
13+
14+
refreshTokenInterceptor(client);
15+
16+
return client;
17+
}
18+
19+
function refreshTokenInterceptor(client) {
20+
const tokenHandler = new RefreshTokenHelper(client);
21+
22+
client.interceptors.response.use((response) => {
23+
return response;
24+
}, (error) => {
25+
const { config, response: { status } } = error;
26+
const originalRequest = config;
27+
const resource = originalRequest.url.replace(originalRequest.baseURL, '');
28+
29+
if (tokenHandler.whitelist.includes(resource)) {
30+
return Promise.reject(error);
31+
}
32+
33+
if (status === 401) {
34+
if (!tokenHandler.isRefreshing) {
35+
tokenHandler.fireRefreshTokenRequest().catch(() => {
36+
return Promise.reject(error);
37+
});
38+
}
39+
40+
return new Promise((resolve, reject) => {
41+
tokenHandler.subscribe((newToken) => {
42+
// replace the expired token and retry
43+
originalRequest.headers.Authorization = `Bearer ${newToken}`;
44+
originalRequest.url = originalRequest.url.replace(originalRequest.baseURL, '');
45+
resolve(Axios(originalRequest));
46+
}, (err) => {
47+
reject(err);
48+
});
49+
});
50+
}
51+
52+
return Promise.reject(error);
53+
});
54+
}

0 commit comments

Comments
 (0)