@nextplus/js-sdk 0.0.12 → 1.14.47
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 +58 -404
- package/dist/{chunk-ASHZA665.js → chunk-4KGK47D7.js} +17 -1
- package/dist/chunk-ILHEBJ5E.js +224 -0
- package/dist/chunk-WJITI4FW.js +41908 -0
- package/dist/index-D-jBrt2u.d.cts +9120 -0
- package/dist/index-UkXqlzcJ.d.ts +9120 -0
- package/dist/index.cjs +29258 -8711
- package/dist/index.d.cts +27 -41
- package/dist/index.d.ts +27 -41
- package/dist/index.js +8 -530
- package/dist/sdk/index.cjs +29583 -8714
- package/dist/sdk/index.d.cts +1 -1
- package/dist/sdk/index.d.ts +1 -1
- package/dist/sdk/index.js +18 -2
- package/dist/sdk/sdk.gen.cjs +29583 -8714
- package/dist/sdk/sdk.gen.d.cts +1 -1
- package/dist/sdk/sdk.gen.d.ts +1 -1
- package/dist/sdk/sdk.gen.js +17 -1
- package/dist/sdk.gen-BA9pswJ_.d.cts +72608 -0
- package/dist/sdk.gen-Dnijnh11.d.ts +72608 -0
- package/dist/smoke.cjs +42797 -0
- package/dist/smoke.d.cts +2 -0
- package/dist/smoke.d.ts +2 -0
- package/dist/smoke.js +79 -0
- package/package.json +4 -12
- package/src/index.ts +76 -256
- package/src/sdk/schemas.gen.ts +2932 -2167
- package/src/sdk/sdk.gen.ts +45971 -10452
- package/src/sdk/transformers.gen.ts +9032 -2705
- package/src/sdk/types.gen.ts +52768 -16446
- package/src/smoke.ts +71 -582
- package/dist/chunk-PEBCO4EJ.js +0 -21048
- package/dist/index-9b6z3K2x.d.cts +0 -4683
- package/dist/index-NQKQN8wR.d.ts +0 -4683
- package/dist/sdk.gen-P3Z3oO-Q.d.ts +0 -36063
- package/dist/sdk.gen-kGvktS0r.d.cts +0 -36063
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import {
|
|
2
|
+
sdk_exports
|
|
3
|
+
} from "./chunk-4KGK47D7.js";
|
|
4
|
+
import {
|
|
5
|
+
UserModelService
|
|
6
|
+
} from "./chunk-WJITI4FW.js";
|
|
7
|
+
import {
|
|
8
|
+
__publicField,
|
|
9
|
+
createClient
|
|
10
|
+
} from "./chunk-FXXZNC42.js";
|
|
11
|
+
|
|
12
|
+
// src/index.ts
|
|
13
|
+
import debug from "debug";
|
|
14
|
+
var log = debug("nextplus:sdk");
|
|
15
|
+
var accessToken = {
|
|
16
|
+
token: null,
|
|
17
|
+
created: null,
|
|
18
|
+
ttl: 0
|
|
19
|
+
};
|
|
20
|
+
var setAccessToken = (token, created = /* @__PURE__ */ new Date(), ttl = 1209600) => {
|
|
21
|
+
accessToken.token = token;
|
|
22
|
+
accessToken.created = created;
|
|
23
|
+
accessToken.ttl = ttl;
|
|
24
|
+
};
|
|
25
|
+
var getAccessToken = () => {
|
|
26
|
+
return accessToken;
|
|
27
|
+
};
|
|
28
|
+
var isTokenValid = () => {
|
|
29
|
+
if (!accessToken.token || !accessToken.created) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
const now = /* @__PURE__ */ new Date();
|
|
33
|
+
const tokenAge = now.getTime() - accessToken.created.getTime();
|
|
34
|
+
const tokenTtlMs = accessToken.ttl * 1e3;
|
|
35
|
+
return tokenAge < tokenTtlMs;
|
|
36
|
+
};
|
|
37
|
+
var NextPlusSDK = class {
|
|
38
|
+
constructor(options) {
|
|
39
|
+
__publicField(this, "client");
|
|
40
|
+
__publicField(this, "credentials");
|
|
41
|
+
__publicField(this, "returnType", "data");
|
|
42
|
+
if (!options.email && !options.username) {
|
|
43
|
+
throw new Error("Either email or username must be provided");
|
|
44
|
+
}
|
|
45
|
+
if (options.email && options.username) {
|
|
46
|
+
throw new Error("Provide either email or username, not both");
|
|
47
|
+
}
|
|
48
|
+
this.credentials = {
|
|
49
|
+
email: options.email,
|
|
50
|
+
username: options.username,
|
|
51
|
+
password: options.password
|
|
52
|
+
};
|
|
53
|
+
this.returnType = options.returnType || "data";
|
|
54
|
+
if (options.baseURL.endsWith("/")) {
|
|
55
|
+
options.baseURL = options.baseURL.slice(0, -1);
|
|
56
|
+
}
|
|
57
|
+
if (!options.baseURL.endsWith("/api")) {
|
|
58
|
+
options.baseURL = options.baseURL + "/api";
|
|
59
|
+
}
|
|
60
|
+
this.client = createClient({
|
|
61
|
+
baseUrl: options.baseURL
|
|
62
|
+
});
|
|
63
|
+
this.client.interceptors.request.use(this.authRequestInterceptor.bind(this));
|
|
64
|
+
this.createServiceProxies();
|
|
65
|
+
}
|
|
66
|
+
// Wrap plain developer inputs into the right container (body/query)
|
|
67
|
+
// and stringify complex query params when required by LoopBack (filter/where)
|
|
68
|
+
normalizeOptions(methodName, rawOptions) {
|
|
69
|
+
const options = rawOptions && typeof rawOptions === "object" ? { ...rawOptions } : {};
|
|
70
|
+
const method = methodName.toString().toLowerCase();
|
|
71
|
+
const isFind = method.includes("find") && !method.includes("count");
|
|
72
|
+
const isCount = method.includes("count");
|
|
73
|
+
const shouldUseBody = /(create|update|replace|patch|login|upsert)/i.test(methodName.toString());
|
|
74
|
+
const topLevelKeys = ["filter", "where", "fields", "order", "limit", "offset", "include"];
|
|
75
|
+
const hasTopLevelFilterBits = topLevelKeys.some((k) => Object.prototype.hasOwnProperty.call(options, k));
|
|
76
|
+
if (isFind) {
|
|
77
|
+
options.query = options.query || {};
|
|
78
|
+
if (options.filter && typeof options.filter === "object") {
|
|
79
|
+
options.query.filter = JSON.stringify(options.filter);
|
|
80
|
+
delete options.filter;
|
|
81
|
+
} else if (hasTopLevelFilterBits) {
|
|
82
|
+
const filterObject = {};
|
|
83
|
+
if (options.where !== void 0) filterObject.where = options.where;
|
|
84
|
+
if (options.fields !== void 0) filterObject.fields = options.fields;
|
|
85
|
+
if (options.order !== void 0) filterObject.order = options.order;
|
|
86
|
+
if (options.limit !== void 0) filterObject.limit = options.limit;
|
|
87
|
+
if (options.offset !== void 0) filterObject.offset = options.offset;
|
|
88
|
+
if (options.include !== void 0) filterObject.include = options.include;
|
|
89
|
+
if (Object.keys(filterObject).length > 0) {
|
|
90
|
+
options.query.filter = typeof filterObject === "string" ? filterObject : JSON.stringify(filterObject);
|
|
91
|
+
}
|
|
92
|
+
topLevelKeys.forEach((k) => delete options[k]);
|
|
93
|
+
}
|
|
94
|
+
} else if (isCount) {
|
|
95
|
+
options.query = options.query || {};
|
|
96
|
+
if (options.where !== void 0) {
|
|
97
|
+
const whereVal = options.where;
|
|
98
|
+
options.query.where = typeof whereVal === "string" ? whereVal : JSON.stringify(whereVal);
|
|
99
|
+
delete options.where;
|
|
100
|
+
}
|
|
101
|
+
} else if (shouldUseBody) {
|
|
102
|
+
if (options.body === void 0) {
|
|
103
|
+
const bodyCandidate = { ...options };
|
|
104
|
+
delete bodyCandidate.client;
|
|
105
|
+
delete bodyCandidate.query;
|
|
106
|
+
delete bodyCandidate.path;
|
|
107
|
+
if (Object.keys(bodyCandidate).length > 0) {
|
|
108
|
+
options.body = bodyCandidate;
|
|
109
|
+
Object.keys(options).forEach((k) => {
|
|
110
|
+
if (!["body", "client", "query", "path"].includes(k)) delete options[k];
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (options.query && typeof options.query === "object") {
|
|
116
|
+
Object.keys(options.query).forEach((key) => {
|
|
117
|
+
const val = options.query[key];
|
|
118
|
+
if (val && (Array.isArray(val) || typeof val === "object")) {
|
|
119
|
+
options.query[key] = JSON.stringify(val);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
return options;
|
|
124
|
+
}
|
|
125
|
+
async authRequestInterceptor(request, _options) {
|
|
126
|
+
if (request.url.includes("/UserModels/login")) {
|
|
127
|
+
return request;
|
|
128
|
+
}
|
|
129
|
+
await this.ensureValidToken();
|
|
130
|
+
const token = getAccessToken();
|
|
131
|
+
if (token.token) {
|
|
132
|
+
request.headers.set("Authorization", token.token);
|
|
133
|
+
}
|
|
134
|
+
return request;
|
|
135
|
+
}
|
|
136
|
+
async ensureValidToken() {
|
|
137
|
+
if (!isTokenValid()) {
|
|
138
|
+
log("Token expired or missing, refreshing...");
|
|
139
|
+
await this.login();
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
async login() {
|
|
143
|
+
try {
|
|
144
|
+
const loginBody = {
|
|
145
|
+
password: this.credentials.password,
|
|
146
|
+
forceLogin: true
|
|
147
|
+
};
|
|
148
|
+
if (this.credentials.email) {
|
|
149
|
+
loginBody.email = this.credentials.email;
|
|
150
|
+
} else if (this.credentials.username) {
|
|
151
|
+
loginBody.username = this.credentials.username;
|
|
152
|
+
}
|
|
153
|
+
const response = await UserModelService.login({
|
|
154
|
+
client: this.client,
|
|
155
|
+
body: loginBody
|
|
156
|
+
});
|
|
157
|
+
if (response.data) {
|
|
158
|
+
const token = response.data.id;
|
|
159
|
+
const ttl = response.data.ttl || 1209600;
|
|
160
|
+
if (token) {
|
|
161
|
+
setAccessToken(token, /* @__PURE__ */ new Date(), ttl);
|
|
162
|
+
log("Login successful, access token set");
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
throw new Error("Login failed - no token received");
|
|
167
|
+
} catch (error) {
|
|
168
|
+
console.error("Login failed:", error);
|
|
169
|
+
throw error;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
createServiceProxies() {
|
|
173
|
+
const serviceNames = Object.keys(sdk_exports).filter(
|
|
174
|
+
(key) => key.endsWith("Service") && typeof sdk_exports[key] === "function"
|
|
175
|
+
);
|
|
176
|
+
serviceNames.forEach((serviceName) => {
|
|
177
|
+
const ServiceClass = sdk_exports[serviceName];
|
|
178
|
+
this[serviceName] = this.createServiceProxy(ServiceClass);
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
createServiceProxy(ServiceClass) {
|
|
182
|
+
const self = this;
|
|
183
|
+
return new Proxy(ServiceClass, {
|
|
184
|
+
get(target, prop) {
|
|
185
|
+
if (typeof target[prop] === "function") {
|
|
186
|
+
return async function(...args) {
|
|
187
|
+
if (!args[0]) args[0] = {};
|
|
188
|
+
args[0] = self.normalizeOptions(String(prop), args[0]);
|
|
189
|
+
if (!args[0].client) args[0].client = self.client;
|
|
190
|
+
const result = await target[prop].apply(target, args);
|
|
191
|
+
if (self.returnType === "data" && result && typeof result === "object" && "data" in result) {
|
|
192
|
+
return result.data;
|
|
193
|
+
}
|
|
194
|
+
if (self.returnType === "data" && result && typeof result === "object" && "error" in result) {
|
|
195
|
+
const error = new Error(result.error.error.message || result.error.error.code || "Unknown error");
|
|
196
|
+
if (result.error.error.details) {
|
|
197
|
+
error.details = result.error.error.details;
|
|
198
|
+
}
|
|
199
|
+
throw error;
|
|
200
|
+
}
|
|
201
|
+
return result;
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
return target[prop];
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
// Manual login method (optional, for explicit control)
|
|
209
|
+
async manualLogin() {
|
|
210
|
+
return this.login();
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
var configure = (options) => {
|
|
214
|
+
return new NextPlusSDK(options);
|
|
215
|
+
};
|
|
216
|
+
var createSDK = (options) => {
|
|
217
|
+
return new NextPlusSDK(options);
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
export {
|
|
221
|
+
NextPlusSDK,
|
|
222
|
+
configure,
|
|
223
|
+
createSDK
|
|
224
|
+
};
|