This SDK is written in pure Typescript with portability in mind being able to use it on NodeJs, Deno, Cloudflare Worker or other runtimes.
- Provides registration process for app
- Verify and signing of requests / responses
- preconfigured API Client
- Complete Registration Handshake between Shopware and this
See examples/node
folder for a full example
import {AppServer, AppConfigurationInterface, InMemoryShopRepository, WebCryptoHmacSigner} from "@friendsofshopware/app-server-sdk";
import express from 'express';
import {convertRequest, convertResponse, rawRequestMiddleware} from '@friendsofshopware/app-server-sdk-express';
const app = express();
const cfg: AppConfigurationInterface = {
appName: 'Test',
appSecret: 'testSecret',
authorizeCallbackUrl: 'http://app-server.dev.localhost/authorize/callback'
};
const appServer = new AppServer(cfg, new InMemoryShopRepository, new WebCryptoHmacSigner);
app.use(rawRequestMiddleware);
app.get('/authorize', async (req, res) => {
const resp = await appServer.registration.authorize(convertRequest(req));
convertResponse(resp, res);
});
app.post('/authorize/callback', async (req, res) => {
const resp = await appServer.registration.authorizeCallback(convertRequest(req));
convertResponse(resp, res);
});
app.post('/event/product-changed', async (req, res) => {
const context = await appServer.contextResolver.fromSource(convertRequest(req));
console.log(await context.httpClient.post('/search/product'));
res.send();
});
app.listen(process.env.PORT || 8080, () => {
console.log(`App listening at http://0.0.0.0:${process.env.PORT || 8080}`)
})
See examples/cf-worker
folder for a full example
import {App} from "shopware-app-server-sdk";
import {Config} from "shopware-app-server-sdk/config";
import {WebCryptoHmacSigner} from "shopware-app-server-sdk/component/signer";
import {convertRequest, convertResponse, CloudflareShopRepository} from "shopware-app-server-sdk/runtime/cf-worker";
const cfg: Config = {
appName: 'Test',
appSecret: 'testSecret',
authorizeCallbackUrl: 'https://xxxx.shyim.workers.dev/authorize/callback'
};
export async function handleRequest(request: Request): Promise<Response> {
const url = new URL(request.url);
// This requires that an KV storage has been bound to shopStorage
// @ts-ignore
const app = new App(cfg, new CloudflareShopRepository(globalThis.shopStorage), new WebCryptoHmacSigner());
if (url.pathname.startsWith('/authorize/callback')) {
const req = await convertRequest(request);
return await convertResponse(await app.registration.authorizeCallback(req));
}
if (url.pathname.startsWith('/authorize')) {
const req = await convertRequest(request);
return await convertResponse(await app.registration.authorize(req));
}
return new Response(`Not found`)
}