@teamnovu/kit-shopware-api-client 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,20 @@
1
+ import { OperationOptions, OperationProp, ShopwareClientOptions } from './types';
2
+ export declare class ShopwareApiError extends Error {
3
+ readonly status: number;
4
+ readonly response?: unknown | undefined;
5
+ constructor(message: string, status: number, response?: unknown | undefined);
6
+ }
7
+ export declare class ShopwareClient<Operations extends Record<string, {
8
+ body?: unknown;
9
+ response?: unknown;
10
+ }>> {
11
+ private options;
12
+ constructor(options: ShopwareClientOptions);
13
+ setBaseURL(baseURL: string): void;
14
+ setApiKey(apiKey: string): void;
15
+ setLanguage(language: string | undefined): void;
16
+ setIncludeSeoUrls(includeSeoUrls?: boolean): void;
17
+ private parseOperation;
18
+ private interpolateUrl;
19
+ query<OperationKey extends (keyof Operations) & string>(operation: OperationKey, options?: OperationOptions<Operations, OperationKey>): Promise<OperationProp<Operations, OperationKey, 'response'>>;
20
+ }
@@ -0,0 +1,2 @@
1
+ export * from './client';
2
+ export * from './types';
package/dist/index.mjs ADDED
@@ -0,0 +1,89 @@
1
+ var h = Object.defineProperty;
2
+ var l = (s, e, t) => e in s ? h(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t;
3
+ var n = (s, e, t) => l(s, typeof e != "symbol" ? e + "" : e, t);
4
+ class o extends Error {
5
+ constructor(e, t, r) {
6
+ super(e), this.status = t, this.response = r, this.name = "ShopwareApiError";
7
+ }
8
+ }
9
+ const c = /^(?<name>\w+?)\s(?<method>get|post|put|patch|delete)\s+(?<url>\/.*)/i;
10
+ class g {
11
+ constructor(e) {
12
+ n(this, "options", {
13
+ baseURL: "",
14
+ apiKey: "",
15
+ language: "",
16
+ includeSeoUrls: !1
17
+ });
18
+ this.setBaseURL(e.baseURL), this.setApiKey(e.apiKey), this.setLanguage(e.language), this.setIncludeSeoUrls(e.includeSeoUrls), this.options = e;
19
+ }
20
+ setBaseURL(e) {
21
+ if (typeof e != "string")
22
+ throw new Error("Invalid baseURL: It must be a string.");
23
+ this.options.baseURL = e;
24
+ }
25
+ setApiKey(e) {
26
+ if (typeof e != "string")
27
+ throw new Error("Invalid apiKey: It must be a string.");
28
+ this.options.apiKey = e;
29
+ }
30
+ setLanguage(e) {
31
+ if (e && typeof e != "string")
32
+ throw new Error("Invalid language: If provided, it must be a string.");
33
+ this.options.language = e;
34
+ }
35
+ setIncludeSeoUrls(e = !0) {
36
+ this.options.includeSeoUrls = e;
37
+ }
38
+ parseOperation(e) {
39
+ const t = c.exec(e);
40
+ if (!(t != null && t.groups))
41
+ throw new Error("Invalid operation format");
42
+ return {
43
+ name: t.groups.name,
44
+ method: t.groups.method.toUpperCase(),
45
+ url: t.groups.url
46
+ };
47
+ }
48
+ interpolateUrl(e, t) {
49
+ return e.replace(/{([^}]+?)}/g, (r, a) => {
50
+ if (!(a in t))
51
+ throw new Error(`Missing required URL parameter: ${a}`);
52
+ return t[a];
53
+ });
54
+ }
55
+ async query(e, t) {
56
+ try {
57
+ const { method: r, url: a } = this.parseOperation(e), u = this.interpolateUrl(a, (t == null ? void 0 : t.params) ?? {}), i = await fetch(`${this.options.baseURL}/store-api${u}`, {
58
+ ...t,
59
+ method: r,
60
+ headers: {
61
+ "Content-Type": "application/json",
62
+ "sw-access-key": this.options.apiKey,
63
+ ...this.options.includeSeoUrls && { "sw-include-seo-urls": "true" },
64
+ ...this.options.language && { "sw-language-id": this.options.language },
65
+ ...t == null ? void 0 : t.headers
66
+ },
67
+ body: t != null && t.body ? JSON.stringify(t.body) : void 0
68
+ });
69
+ if (!i.ok)
70
+ throw new o(
71
+ `Shopware API request failed: ${i.statusText}`,
72
+ i.status,
73
+ await i.json().catch(() => {
74
+ })
75
+ );
76
+ return await i.json();
77
+ } catch (r) {
78
+ throw r instanceof o ? r : new o(
79
+ "Failed to communicate with Shopware API",
80
+ 500,
81
+ r instanceof Error ? r.message : void 0
82
+ );
83
+ }
84
+ }
85
+ }
86
+ export {
87
+ o as ShopwareApiError,
88
+ g as ShopwareClient
89
+ };
@@ -0,0 +1,26 @@
1
+ export type HttpMethod = 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE';
2
+ type ExtractUrlParams<URL extends string> = URL extends `${infer _Start}/{${infer Param}}${infer Rest}` ? Param | ExtractUrlParams<Rest> : never;
3
+ type UrlParamsRecord<URL extends string> = {
4
+ [K in ExtractUrlParams<URL>]: string;
5
+ };
6
+ export type Operation<Operations, OperationKey extends keyof Operations> = Operations[OperationKey];
7
+ export type OperationProp<Operations, OperationKey extends keyof Operations, Prop extends string> = Operation<Operations, OperationKey> extends {
8
+ [K in Prop]?: unknown;
9
+ } ? Operation<Operations, OperationKey>[Prop] : never;
10
+ export type InferParameters<T extends string> = UrlParamsRecord<ExtractUrlFromOperation<T>>;
11
+ type OperationKey<Operations> = keyof Operations & string;
12
+ type ExtractUrlFromOperation<K extends string> = K extends `${string} ${string} ${infer U}` ? U : never;
13
+ export type OperationOptions<Operations, K extends OperationKey<Operations>> = Omit<RequestInit, 'body' | 'method'> & {
14
+ body?: OperationProp<Operations, K, 'body'>;
15
+ } & (UrlParamsRecord<ExtractUrlFromOperation<K>> extends infer T ? (keyof T extends never ? {
16
+ params?: void;
17
+ } : {
18
+ params: T;
19
+ }) : never);
20
+ export interface ShopwareClientOptions {
21
+ baseURL: string;
22
+ apiKey: string;
23
+ includeSeoUrls?: boolean;
24
+ language?: string;
25
+ }
26
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teamnovu/kit-shopware-api-client",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "A client for the Shopware API",
5
5
  "main": "dist/index.mjs",
6
6
  "module": "dist/index.mjs",
@@ -14,11 +14,6 @@
14
14
  "peerDependencies": {
15
15
  "vue": "^3.0.0"
16
16
  },
17
- "scripts": {
18
- "build": "vite build",
19
- "watch": "NODE_ENV=development vite build --watch",
20
- "lint": "eslint --fix --ignore-pattern 'dist/**' ."
21
- },
22
17
  "repository": {
23
18
  "type": "git",
24
19
  "url": "git+ssh://git@github.com:teamnovu/kit.git"
@@ -26,5 +21,10 @@
26
21
  "keywords": [],
27
22
  "author": "Elias Bernhaut",
28
23
  "homepage": "https://kit.novu.ch/packages/shopware-api-client/",
29
- "dependencies": {}
24
+ "dependencies": {},
25
+ "scripts": {
26
+ "build": "vite build",
27
+ "watch": "NODE_ENV=development vite build --watch",
28
+ "lint": "eslint --fix --ignore-pattern 'dist/**' ."
29
+ }
30
30
  }