@teamnovu/kit-shopware-api-client 0.0.1
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.
- package/eslint.config.mjs +84 -0
- package/package.json +30 -0
- package/src/client.ts +136 -0
- package/src/index.ts +2 -0
- package/src/types.ts +40 -0
- package/tsconfig.json +12 -0
- package/vite.config.js +32 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import js from '@eslint/js'
|
|
2
|
+
import pluginStylistic from '@stylistic/eslint-plugin'
|
|
3
|
+
import tseslint from 'typescript-eslint'
|
|
4
|
+
|
|
5
|
+
export default tseslint.config(
|
|
6
|
+
js.configs.recommended,
|
|
7
|
+
tseslint.configs.recommended,
|
|
8
|
+
pluginStylistic.configs['recommended'],
|
|
9
|
+
{
|
|
10
|
+
rules: {
|
|
11
|
+
// stylistic
|
|
12
|
+
// ********************
|
|
13
|
+
'@stylistic/newline-per-chained-call': [
|
|
14
|
+
'error',
|
|
15
|
+
{ ignoreChainWithDepth: 2 },
|
|
16
|
+
],
|
|
17
|
+
'@stylistic/nonblock-statement-body-position': 'error',
|
|
18
|
+
'@stylistic/one-var-declaration-per-line': 'error',
|
|
19
|
+
'@stylistic/operator-linebreak': [
|
|
20
|
+
'error',
|
|
21
|
+
'before',
|
|
22
|
+
{ overrides: { '=': 'none' } },
|
|
23
|
+
],
|
|
24
|
+
'@stylistic/function-paren-newline': ['error', 'multiline-arguments'],
|
|
25
|
+
'@stylistic/dot-location': ['error', 'property'],
|
|
26
|
+
'@stylistic/no-mixed-operators': ['error', { allowSamePrecedence: true }],
|
|
27
|
+
'@stylistic/brace-style': ['error', '1tbs', { allowSingleLine: true }],
|
|
28
|
+
'@stylistic/array-bracket-newline': ['error', { multiline: true }],
|
|
29
|
+
'@stylistic/array-element-newline': [
|
|
30
|
+
'error',
|
|
31
|
+
{
|
|
32
|
+
multiline: true,
|
|
33
|
+
consistent: true,
|
|
34
|
+
},
|
|
35
|
+
],
|
|
36
|
+
'@stylistic/max-len': [
|
|
37
|
+
'warn',
|
|
38
|
+
{
|
|
39
|
+
code: 150,
|
|
40
|
+
tabWidth: 2,
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
'@stylistic/object-curly-newline': [
|
|
44
|
+
'error',
|
|
45
|
+
{
|
|
46
|
+
multiline: true,
|
|
47
|
+
consistent: true,
|
|
48
|
+
},
|
|
49
|
+
],
|
|
50
|
+
'@stylistic/object-property-newline': [
|
|
51
|
+
'error',
|
|
52
|
+
{ allowAllPropertiesOnSameLine: false },
|
|
53
|
+
],
|
|
54
|
+
|
|
55
|
+
// common
|
|
56
|
+
// ********************
|
|
57
|
+
'no-console': ['error', { allow: ['warn', 'error'] }],
|
|
58
|
+
'prefer-destructuring': [
|
|
59
|
+
'error',
|
|
60
|
+
{
|
|
61
|
+
array: false,
|
|
62
|
+
object: true,
|
|
63
|
+
},
|
|
64
|
+
],
|
|
65
|
+
'no-unused-vars': 'off',
|
|
66
|
+
'eqeqeq': ['error', 'smart'],
|
|
67
|
+
'no-undef': 'off', // done by typescript
|
|
68
|
+
'@typescript-eslint/consistent-type-imports': 'error',
|
|
69
|
+
'@typescript-eslint/no-explicit-any': 'warn',
|
|
70
|
+
'@typescript-eslint/no-unused-vars': [
|
|
71
|
+
'error',
|
|
72
|
+
{
|
|
73
|
+
args: 'all',
|
|
74
|
+
argsIgnorePattern: '^_',
|
|
75
|
+
caughtErrors: 'all',
|
|
76
|
+
caughtErrorsIgnorePattern: '^_',
|
|
77
|
+
destructuredArrayIgnorePattern: '^_',
|
|
78
|
+
varsIgnorePattern: '^_',
|
|
79
|
+
ignoreRestSiblings: true,
|
|
80
|
+
},
|
|
81
|
+
],
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
)
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@teamnovu/kit-shopware-api-client",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "A client for the Shopware API",
|
|
5
|
+
"main": "dist/index.mjs",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"peerDependencies": {
|
|
15
|
+
"vue": "^3.0.0"
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "vite build",
|
|
19
|
+
"watch": "NODE_ENV=development vite build --watch",
|
|
20
|
+
"lint": "eslint --fix --ignore-pattern 'dist/**' ."
|
|
21
|
+
},
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+ssh://git@github.com:teamnovu/kit.git"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [],
|
|
27
|
+
"author": "Elias Bernhaut",
|
|
28
|
+
"homepage": "https://kit.novu.ch/packages/shopware-api-client/",
|
|
29
|
+
"dependencies": {}
|
|
30
|
+
}
|
package/src/client.ts
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
OperationOptions,
|
|
3
|
+
OperationProp,
|
|
4
|
+
ShopwareClientOptions,
|
|
5
|
+
} from './types'
|
|
6
|
+
|
|
7
|
+
export class ShopwareApiError extends Error {
|
|
8
|
+
constructor(
|
|
9
|
+
message: string,
|
|
10
|
+
public readonly status: number,
|
|
11
|
+
public readonly response?: unknown,
|
|
12
|
+
) {
|
|
13
|
+
super(message)
|
|
14
|
+
this.name = 'ShopwareApiError'
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const operationRegex = /^(?<name>\w+?)\s(?<method>get|post|put|patch|delete)\s+(?<url>\/.*)/i
|
|
19
|
+
|
|
20
|
+
export class ShopwareClient<Operations extends Record<string, {
|
|
21
|
+
body?: unknown
|
|
22
|
+
response?: unknown
|
|
23
|
+
}>> {
|
|
24
|
+
private options: ShopwareClientOptions = {
|
|
25
|
+
baseURL: '',
|
|
26
|
+
apiKey: '',
|
|
27
|
+
language: '',
|
|
28
|
+
includeSeoUrls: false,
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
constructor(options: ShopwareClientOptions) {
|
|
32
|
+
this.setBaseURL(options.baseURL)
|
|
33
|
+
this.setApiKey(options.apiKey)
|
|
34
|
+
this.setLanguage(options.language)
|
|
35
|
+
this.setIncludeSeoUrls(options.includeSeoUrls)
|
|
36
|
+
this.options = options
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public setBaseURL(baseURL: string) {
|
|
40
|
+
if (typeof baseURL !== 'string') {
|
|
41
|
+
throw new Error('Invalid baseURL: It must be a string.')
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
this.options.baseURL = baseURL
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
public setApiKey(apiKey: string) {
|
|
48
|
+
if (typeof apiKey !== 'string') {
|
|
49
|
+
throw new Error('Invalid apiKey: It must be a string.')
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
this.options.apiKey = apiKey
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public setLanguage(language: string | undefined) {
|
|
56
|
+
if (language && typeof language !== 'string') {
|
|
57
|
+
throw new Error('Invalid language: If provided, it must be a string.')
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
this.options.language = language
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public setIncludeSeoUrls(includeSeoUrls: boolean = true) {
|
|
64
|
+
this.options.includeSeoUrls = includeSeoUrls
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
private parseOperation(operation: keyof Operations & string): {
|
|
68
|
+
name: string
|
|
69
|
+
method: string
|
|
70
|
+
url: string
|
|
71
|
+
} {
|
|
72
|
+
const match = operationRegex.exec(operation)
|
|
73
|
+
if (!match?.groups) {
|
|
74
|
+
throw new Error('Invalid operation format')
|
|
75
|
+
}
|
|
76
|
+
return {
|
|
77
|
+
name: match.groups.name,
|
|
78
|
+
method: match.groups.method.toUpperCase(),
|
|
79
|
+
url: match.groups.url,
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
private interpolateUrl(url: string, params: Record<string, string>): string {
|
|
84
|
+
return url.replace(/{([^}]+?)}/g, (_, param) => {
|
|
85
|
+
if (!(param in params)) {
|
|
86
|
+
throw new Error(`Missing required URL parameter: ${param}`)
|
|
87
|
+
}
|
|
88
|
+
return params[param]
|
|
89
|
+
})
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async query<OperationKey extends (keyof Operations) & string>(
|
|
93
|
+
operation: OperationKey,
|
|
94
|
+
options?: OperationOptions<Operations, OperationKey>,
|
|
95
|
+
): Promise<OperationProp<Operations, OperationKey, 'response'>> {
|
|
96
|
+
try {
|
|
97
|
+
const { method, url } = this.parseOperation(operation)
|
|
98
|
+
|
|
99
|
+
const interpolatedUrl = this.interpolateUrl(url, options?.params ?? {})
|
|
100
|
+
|
|
101
|
+
const response = await fetch(`${this.options.baseURL}/store-api${interpolatedUrl}`, {
|
|
102
|
+
...options,
|
|
103
|
+
method,
|
|
104
|
+
headers: {
|
|
105
|
+
'Content-Type': 'application/json',
|
|
106
|
+
'sw-access-key': this.options.apiKey,
|
|
107
|
+
...(this.options.includeSeoUrls && { 'sw-include-seo-urls': 'true' }),
|
|
108
|
+
...(this.options.language && { 'sw-language-id': this.options.language }),
|
|
109
|
+
...options?.headers,
|
|
110
|
+
},
|
|
111
|
+
body: options?.body ? JSON.stringify(options.body) : undefined,
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
if (!response.ok) {
|
|
115
|
+
throw new ShopwareApiError(
|
|
116
|
+
`Shopware API request failed: ${response.statusText}`,
|
|
117
|
+
response.status,
|
|
118
|
+
await response.json().catch(() => undefined),
|
|
119
|
+
)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const data = await response.json()
|
|
123
|
+
return data
|
|
124
|
+
} catch (error) {
|
|
125
|
+
if (error instanceof ShopwareApiError) {
|
|
126
|
+
throw error
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
throw new ShopwareApiError(
|
|
130
|
+
'Failed to communicate with Shopware API',
|
|
131
|
+
500,
|
|
132
|
+
error instanceof Error ? error.message : undefined,
|
|
133
|
+
)
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
package/src/index.ts
ADDED
package/src/types.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export type HttpMethod = 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE'
|
|
2
|
+
|
|
3
|
+
// Extract URL parameters from a URL pattern
|
|
4
|
+
type ExtractUrlParams<URL extends string> = URL extends `${infer _Start}/{${infer Param}}${infer Rest}`
|
|
5
|
+
? Param | ExtractUrlParams<Rest>
|
|
6
|
+
: never
|
|
7
|
+
|
|
8
|
+
// Create a record type of required URL parameters
|
|
9
|
+
type UrlParamsRecord<URL extends string> = {
|
|
10
|
+
[K in ExtractUrlParams<URL>]: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type Operation<Operations, OperationKey extends keyof Operations> = Operations[OperationKey]
|
|
14
|
+
export type OperationProp<
|
|
15
|
+
Operations,
|
|
16
|
+
OperationKey extends keyof Operations,
|
|
17
|
+
Prop extends string,
|
|
18
|
+
> = Operation<Operations, OperationKey> extends { [K in Prop]?: unknown } ? Operation<Operations, OperationKey>[Prop] : never
|
|
19
|
+
|
|
20
|
+
export type InferParameters<T extends string> = UrlParamsRecord<ExtractUrlFromOperation<T>>
|
|
21
|
+
|
|
22
|
+
type OperationKey<Operations> = keyof Operations & string
|
|
23
|
+
type ExtractUrlFromOperation<K extends string> = K extends `${string} ${string} ${infer U}` ? U : never
|
|
24
|
+
|
|
25
|
+
export type OperationOptions<
|
|
26
|
+
Operations,
|
|
27
|
+
K extends OperationKey<Operations>,
|
|
28
|
+
> = Omit<RequestInit, 'body' | 'method'> & {
|
|
29
|
+
body?: OperationProp<Operations, K, 'body'>
|
|
30
|
+
} &
|
|
31
|
+
(UrlParamsRecord<ExtractUrlFromOperation<K>> extends infer T
|
|
32
|
+
? (keyof T extends never ? { params?: void } : { params: T })
|
|
33
|
+
: never)
|
|
34
|
+
|
|
35
|
+
export interface ShopwareClientOptions {
|
|
36
|
+
baseURL: string
|
|
37
|
+
apiKey: string
|
|
38
|
+
includeSeoUrls?: boolean
|
|
39
|
+
language?: string
|
|
40
|
+
}
|
package/tsconfig.json
ADDED
package/vite.config.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import vue from '@vitejs/plugin-vue'
|
|
2
|
+
import { resolve } from 'path'
|
|
3
|
+
import { defineConfig } from 'vite'
|
|
4
|
+
import dts from 'vite-plugin-dts'
|
|
5
|
+
import pkg from './package.json'
|
|
6
|
+
|
|
7
|
+
export default defineConfig({
|
|
8
|
+
plugins: [vue(), dts()],
|
|
9
|
+
root: resolve(__dirname),
|
|
10
|
+
build: {
|
|
11
|
+
emptyOutDir: false,
|
|
12
|
+
lib: {
|
|
13
|
+
formats: ['es'],
|
|
14
|
+
// Could also be a dictionary or array of multiple entry points
|
|
15
|
+
entry: {
|
|
16
|
+
index: resolve(__dirname, 'src/index.ts'),
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
rollupOptions: {
|
|
20
|
+
// make sure to externalize deps that shouldn't be bundled
|
|
21
|
+
// into your library
|
|
22
|
+
external: ['vue', ...Object.keys(pkg.dependencies ?? {})],
|
|
23
|
+
output: {
|
|
24
|
+
// Provide global variables to use in the UMD build
|
|
25
|
+
// for externalized deps
|
|
26
|
+
globals: {
|
|
27
|
+
vue: 'Vue',
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
})
|