@shopware/api-client 0.3.0 → 0.5.0
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/README.md +61 -5
- package/admin-api-types/apiSchema-6.5.3.0.json +101263 -0
- package/admin-api-types/apiTypes-6.5.3.0.d.ts +59568 -0
- package/admin-api-types/index.d.ts +1 -0
- package/dist/index.cjs +245 -0
- package/dist/index.d.cts +66371 -0
- package/dist/index.d.mts +66371 -0
- package/dist/index.d.ts +59283 -665
- package/dist/index.mjs +167 -33
- package/package.json +20 -8
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./apiTypes-6.5.3.0";
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const ofetch = require('ofetch');
|
|
4
|
+
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
7
|
+
var __publicField = (obj, key, value) => {
|
|
8
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
9
|
+
return value;
|
|
10
|
+
};
|
|
11
|
+
class ApiClientError extends Error {
|
|
12
|
+
constructor(response) {
|
|
13
|
+
let message = "Failed request";
|
|
14
|
+
message += response._data?.errors.reduce((message2, error) => {
|
|
15
|
+
let pointer = "";
|
|
16
|
+
if (error.source?.pointer) {
|
|
17
|
+
pointer = `[${error.source.pointer}]`;
|
|
18
|
+
}
|
|
19
|
+
return `${message2}
|
|
20
|
+
- [${error.title}]${pointer} ${error.detail ?? ""}`;
|
|
21
|
+
}, "");
|
|
22
|
+
super(message);
|
|
23
|
+
/**
|
|
24
|
+
* Flag to indicate if the request was successful.
|
|
25
|
+
*/
|
|
26
|
+
__publicField(this, "ok");
|
|
27
|
+
/**
|
|
28
|
+
* HTTP status code of the response.
|
|
29
|
+
*/
|
|
30
|
+
__publicField(this, "status");
|
|
31
|
+
/**
|
|
32
|
+
* HTTP status text of the response.
|
|
33
|
+
*/
|
|
34
|
+
__publicField(this, "statusText");
|
|
35
|
+
/**
|
|
36
|
+
* URL of the request.
|
|
37
|
+
*/
|
|
38
|
+
__publicField(this, "url");
|
|
39
|
+
/**
|
|
40
|
+
* Details of the error.
|
|
41
|
+
*/
|
|
42
|
+
__publicField(this, "details");
|
|
43
|
+
/**
|
|
44
|
+
* Headers of the response.
|
|
45
|
+
*/
|
|
46
|
+
__publicField(this, "headers");
|
|
47
|
+
this.name = "ApiClientError";
|
|
48
|
+
this.details = response._data || {
|
|
49
|
+
errors: [{ title: "Unknown error", detail: "" }]
|
|
50
|
+
};
|
|
51
|
+
this.ok = response.ok;
|
|
52
|
+
this.status = response.status;
|
|
53
|
+
this.statusText = response.statusText;
|
|
54
|
+
this.url = response.url;
|
|
55
|
+
this.headers = response.headers;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function errorInterceptor(response) {
|
|
59
|
+
throw new ApiClientError(response);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function transformPathToQuery(path, params) {
|
|
63
|
+
const [, method, pathDefinition, headerParams] = path.split(" ");
|
|
64
|
+
const [requestPath, queryParams] = pathDefinition.split("?");
|
|
65
|
+
const pathParams = requestPath.match(/{[^}]+}/g)?.map((param) => param.substring(1, param.length - 1)) || [];
|
|
66
|
+
const requestPathWithParams = pathParams.reduce((acc, paramName) => {
|
|
67
|
+
return acc.replace(`{${paramName}}`, params[paramName]);
|
|
68
|
+
}, requestPath);
|
|
69
|
+
const queryParamNames = queryParams?.split(",") || [];
|
|
70
|
+
const headerParamnames = headerParams?.split(",") || [];
|
|
71
|
+
const headers = {};
|
|
72
|
+
headerParamnames.forEach((paramName) => {
|
|
73
|
+
headers[paramName] = params[paramName];
|
|
74
|
+
});
|
|
75
|
+
const query = {};
|
|
76
|
+
queryParamNames.forEach((paramName) => {
|
|
77
|
+
let queryParamName = paramName;
|
|
78
|
+
if (Array.isArray(params[paramName]) && !queryParamName.includes("[]")) {
|
|
79
|
+
queryParamName += "[]";
|
|
80
|
+
}
|
|
81
|
+
query[queryParamName] = params[paramName];
|
|
82
|
+
});
|
|
83
|
+
const returnOptions = {
|
|
84
|
+
method: method.toUpperCase(),
|
|
85
|
+
headers,
|
|
86
|
+
query
|
|
87
|
+
};
|
|
88
|
+
if (!params) {
|
|
89
|
+
return [requestPathWithParams, returnOptions];
|
|
90
|
+
}
|
|
91
|
+
Object.keys(params).forEach((key) => {
|
|
92
|
+
if (!pathParams.includes(key) && !queryParamNames.includes(key) && !headerParamnames.includes(key)) {
|
|
93
|
+
returnOptions.body ?? (returnOptions.body = {});
|
|
94
|
+
Reflect.set(returnOptions.body, key, params[key]);
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
return [requestPathWithParams, returnOptions];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function createAPIClient(params) {
|
|
101
|
+
const defaultHeaders = {
|
|
102
|
+
"sw-access-key": params.accessToken
|
|
103
|
+
};
|
|
104
|
+
if (params.contextToken) {
|
|
105
|
+
defaultHeaders["sw-context-token"] = params.contextToken;
|
|
106
|
+
}
|
|
107
|
+
const apiFetch = ofetch.ofetch.create({
|
|
108
|
+
baseURL: params.baseURL,
|
|
109
|
+
// async onRequest({ request, options }) {},
|
|
110
|
+
// async onRequestError({ request, options, error }) {},
|
|
111
|
+
async onResponse(context) {
|
|
112
|
+
if (defaultHeaders["sw-context-token"] !== context.response.headers.get("sw-context-token")) {
|
|
113
|
+
const newContextToken = context.response.headers.get("sw-context-token") || "";
|
|
114
|
+
defaultHeaders["sw-context-token"] = newContextToken;
|
|
115
|
+
params.onContextChanged?.(newContextToken);
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
async onResponseError({ request, response, options }) {
|
|
119
|
+
errorInterceptor(response);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
async function invoke(pathParam, ...params2) {
|
|
123
|
+
const [requestPath, options] = transformPathToQuery(
|
|
124
|
+
pathParam,
|
|
125
|
+
params2?.[0]
|
|
126
|
+
);
|
|
127
|
+
return apiFetch(
|
|
128
|
+
requestPath,
|
|
129
|
+
{
|
|
130
|
+
...options,
|
|
131
|
+
headers: {
|
|
132
|
+
...defaultHeaders,
|
|
133
|
+
...options.headers
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
return {
|
|
139
|
+
invoke
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
function createAuthorizationHeader(token) {
|
|
143
|
+
if (!token)
|
|
144
|
+
return null;
|
|
145
|
+
if (token.startsWith("Bearer "))
|
|
146
|
+
return token;
|
|
147
|
+
return `Bearer ${token}`;
|
|
148
|
+
}
|
|
149
|
+
function createAdminAPIClient(params) {
|
|
150
|
+
const sessionData = {
|
|
151
|
+
accessToken: params.sessionData?.accessToken || "",
|
|
152
|
+
refreshToken: params.sessionData?.refreshToken || "",
|
|
153
|
+
expirationTime: Number(params.sessionData?.expirationTime || 0)
|
|
154
|
+
};
|
|
155
|
+
function updateSessionData(responseData) {
|
|
156
|
+
if (responseData?.access_token) {
|
|
157
|
+
defaultHeaders.Authorization = createAuthorizationHeader(
|
|
158
|
+
responseData.access_token
|
|
159
|
+
);
|
|
160
|
+
sessionData.accessToken = responseData.access_token;
|
|
161
|
+
sessionData.refreshToken = responseData.refresh_token;
|
|
162
|
+
sessionData.expirationTime = Date.now() + responseData.expires_in * 1e3;
|
|
163
|
+
params.onAuthChange?.({
|
|
164
|
+
...sessionData
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
function setSessionData(data) {
|
|
169
|
+
sessionData.accessToken = data.accessToken;
|
|
170
|
+
sessionData.refreshToken = data.refreshToken;
|
|
171
|
+
sessionData.expirationTime = data.expirationTime;
|
|
172
|
+
return getSessionData();
|
|
173
|
+
}
|
|
174
|
+
function getSessionData() {
|
|
175
|
+
return { ...sessionData };
|
|
176
|
+
}
|
|
177
|
+
const defaultHeaders = {
|
|
178
|
+
Authorization: createAuthorizationHeader(sessionData.accessToken)
|
|
179
|
+
};
|
|
180
|
+
const apiFetch = ofetch.ofetch.create({
|
|
181
|
+
baseURL: params.baseURL,
|
|
182
|
+
async onRequest({ request, options }) {
|
|
183
|
+
const isExpired = sessionData.expirationTime <= Date.now();
|
|
184
|
+
if (isExpired && !request.toString().includes("/oauth/token")) {
|
|
185
|
+
await ofetch.ofetch("/oauth/token", {
|
|
186
|
+
baseURL: params.baseURL,
|
|
187
|
+
method: "POST",
|
|
188
|
+
body: {
|
|
189
|
+
grant_type: "refresh_token",
|
|
190
|
+
client_id: "administration",
|
|
191
|
+
refresh_token: sessionData.refreshToken || ""
|
|
192
|
+
},
|
|
193
|
+
headers: defaultHeaders,
|
|
194
|
+
onResponseError({ response }) {
|
|
195
|
+
errorInterceptor(response);
|
|
196
|
+
},
|
|
197
|
+
onResponse(context) {
|
|
198
|
+
updateSessionData(context.response._data);
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
},
|
|
203
|
+
async onResponse(context) {
|
|
204
|
+
updateSessionData(context.response._data);
|
|
205
|
+
},
|
|
206
|
+
async onResponseError({ request, response, options }) {
|
|
207
|
+
errorInterceptor(response);
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
async function invoke(pathParam, params2) {
|
|
211
|
+
const [requestPath, options] = transformPathToQuery(
|
|
212
|
+
pathParam,
|
|
213
|
+
params2
|
|
214
|
+
);
|
|
215
|
+
return apiFetch(
|
|
216
|
+
requestPath,
|
|
217
|
+
{
|
|
218
|
+
...options,
|
|
219
|
+
headers: {
|
|
220
|
+
...defaultHeaders,
|
|
221
|
+
...options.headers
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
return {
|
|
227
|
+
/**
|
|
228
|
+
* Invoke API request based on provided path definition.
|
|
229
|
+
*/
|
|
230
|
+
invoke,
|
|
231
|
+
/**
|
|
232
|
+
* Enables to change session data in runtime. Useful for testing purposes.
|
|
233
|
+
* Setting session data with this methis will **not** fire `onAuthChange` hook.
|
|
234
|
+
*/
|
|
235
|
+
setSessionData,
|
|
236
|
+
/**
|
|
237
|
+
* Returns current session data. Useful for testing purposes, as in most cases you'll want to use `onAuthChange` hook for that.
|
|
238
|
+
*/
|
|
239
|
+
getSessionData
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
exports.ApiClientError = ApiClientError;
|
|
244
|
+
exports.createAPIClient = createAPIClient;
|
|
245
|
+
exports.createAdminAPIClient = createAdminAPIClient;
|