UNPKG

@swishapp/node

Version:

JS library to integrate Swish on a Node.js server.

56 lines (41 loc) 1.6 kB
# Swish app proxy for Node.js The Swish browser library requires a proxy service that operates on the same domain as the store. This service facilitates the loading of additional resources and ensures secure communication with the Swish API. ## Installation ```sh npm i @swishapp/node ``` ## Usage The example below is for Remix and needs to be adjusted for other frameworks. ```js import { ActionFunction, LoaderFunction } from "@remix-run/node"; import { createAppProxy, MemoryStorage } from "@swishapp/node"; const swishProxy = createAppProxy({ basePath: "/swish", // Needs to match the route path authToken: process.env.SWISH_API_TOKEN, storage: new MemoryStorage(), // Use MemoryStorage for development only! authenticate: async (request) => { // Authenticate the request and return customer id or null // Throw an error if the request cannot be authenticted return null; }, onError: (error) => { console.error(error); }, }); export const loader: LoaderFunction = async ({ request }) => swishProxy.forward(request); export const action: ActionFunction = async ({ request }) => swishProxy.forward(request); ``` The Swish app for browsers won't work on the server. Use it as a client only script! ```ts // The proxy also serves the Swish brwoser app import { createApp } from "/swish/assets/swish.js"; // Assuming the proxy runs on /swish const swish = await createApp({ proxy: { baseUrl: "/swish", // Use proxy route path }, }); ``` ## Documentation For more information visit [Swish DEV docs](https://developers.swish.app/libraries/node.js).