dummy-json-sdk-beta
Version:
A SDK for DummyJSON API (Beta version).
278 lines (268 loc) • 8.02 kB
JavaScript
// src/endpoints/Base.ts
var BaseEndpoint = class {
#instance;
constructor(instance) {
this.#instance = instance;
}
mergeHeaders(...headers) {
return Object.assign({}, ...headers);
}
createRequest(options) {
const headers = options?.headers ?? {};
return {
get: (url, query = {}, options2) => this.handleRequest({
url,
method: "GET",
query,
headers: this.mergeHeaders(headers, options2?.headers ?? {})
}),
post: (url, body = null, options2) => this.handleRequest({
url,
method: "POST",
body,
headers: this.mergeHeaders(headers, options2?.headers ?? {})
})
};
}
async handleRequest(options) {
const { baseURL, config } = this.#instance;
const { url, method, query, body, headers, ...restOptions } = options;
const { Authorization, refreshToken, ...configQuey } = config ?? {};
const urlObj = new URL(`${baseURL}/${url}`);
Object.entries({ ...configQuey, ...query ?? {} }).forEach(([k, v]) => {
urlObj.searchParams.append(k, String(v));
});
const input = urlObj.toString();
const baseHeaders = { ...headers || {} };
const mergedHeaders = Authorization ? { ...baseHeaders, Authorization } : baseHeaders;
const baseBody = body ? body : void 0;
const mergedBody = refreshToken ? JSON.stringify({ baseBody, refreshToken }) : JSON.stringify(baseBody);
const init = {
method: method || "GET",
headers: mergedHeaders,
body: mergedBody,
...restOptions
};
const promise = fetch(input, init);
try {
const response = await promise;
const contentType = response.headers.get("content-type");
if (contentType && contentType.includes("application/json")) {
const data = await response.json();
return data;
} else {
const text = await response.text();
const error = `Expected JSON, but got:", ${text.slice(0, 200)}`;
console.error(error);
}
} catch (error) {
throw error;
}
}
};
// src/endpoints/Auth.ts
var AuthEndpoint = class extends BaseEndpoint {
#request = this.createRequest();
login(body) {
return this.#request.post("auth/login", body, {
credentials: "include"
});
}
me() {
return this.#request.get("auth/me", null, {
credentials: "include"
});
}
refresh(body) {
return this.#request.post("auth/refresh", body, {
credentials: "include"
});
}
};
// src/endpoints/Cart.ts
var CartEndpoint = class extends BaseEndpoint {
#request = this.createRequest();
get(arg1, arg2) {
const url = !arg1 ? "carts" : typeof arg1 === "number" ? `carts/${arg1}` : arg2 ? `carts/user/${arg2}` : "carts";
return this.#request.get(url);
}
};
// src/endpoints/Comment.ts
var CommentEndpoint = class extends BaseEndpoint {
#request = this.createRequest();
get(arg1, arg2, arg3) {
let url = "comments";
let query = {};
if (typeof arg1 === "number") {
url = `comments/${arg1}`;
query = arg2 || {};
} else if (typeof arg1 === "string") {
url = `comments/${arg1}/${arg2}`;
query = arg3 || {};
} else if (arg1 && typeof arg1 === "object") {
query = arg1;
}
return this.#request.get(url, query);
}
};
// src/endpoints/Post.ts
var PostEndpoint = class extends BaseEndpoint {
#request = this.createRequest();
get(arg1, arg2, arg3) {
let url = "posts";
let query = {};
if (typeof arg1 === "number") {
url = `posts/${arg1}`;
query = arg2 || {};
} else if (typeof arg1 === "string") {
url = `posts/${arg1}/${arg2}`;
query = arg3 || {};
} else if (arg1 && typeof arg1 === "object") {
query = arg1;
}
return this.#request.get(url, query);
}
tags(type) {
const url = type ? "posts/tag-list" : "posts/tag";
return this.#request.get(url);
}
comments(id, query) {
return this.#request.get(`posts/${id}/comments`, query || {});
}
};
// src/endpoints/Product.ts
var ProductEndpoint = class extends BaseEndpoint {
#request = this.createRequest();
get(arg1, query) {
const url = typeof arg1 === "number" ? `products/${arg1}` : typeof arg1 === "string" ? `product/category/${arg1}` : "products";
return this.#request.get(url, typeof arg1 !== "object" ? query : arg1);
}
search(keywords, query) {
return this.#request.get("products/search", {
q: keywords,
...query
});
}
categories(type) {
const url = typeof type === "string" ? "product/category-list" : "product/categories";
return this.#request.get(url);
}
};
// src/endpoints/quote.ts
var QuoteEndpoint = class extends BaseEndpoint {
#request = this.createRequest();
get(arg1, arg2) {
let url = "quotes";
let query = {};
if (typeof arg1 === "number") {
url = `quotes/${arg1}`;
query = arg2 || {};
} else {
query = arg1 ?? {};
}
return this.#request.get(url, query);
}
random(max) {
const url = max ? `/quotes/random/${max}` : "/quotes/random";
return this.#request.get(url);
}
};
// src/endpoints/Recipe.ts
var RecipeEndpoint = class extends BaseEndpoint {
#request = this.createRequest();
get(arg1, arg2, arg3) {
const url = !arg1 || typeof arg1 === "object" ? "recipes" : typeof arg1 === "number" ? `recipes/${arg1}` : typeof arg1 === "string" && arg2 ? `recipes/${arg1}/${arg2}` : "recipes";
const query = arg1 && typeof arg1 === "object" ? arg1 : arg2 && typeof arg2 === "object" ? arg2 : arg3;
return this.#request.get(url, query || {});
}
};
// src/endpoints/Todo.ts
var TodoEndpoint = class extends BaseEndpoint {
#request = this.createRequest();
get(arg1, arg2, arg3) {
let url = "todos";
let query = {};
if (typeof arg1 === "number") {
url = `comments/${arg1}`;
query = arg2 || {};
} else if (typeof arg1 === "string") {
url = `comments/${arg1}/${arg2}`;
query = arg3 || {};
} else if (arg1 && typeof arg1 === "object") {
query = arg1;
}
return this.#request.get(url, query);
}
random(max) {
const url = max ? `todos/random/${max}` : "todos/random";
return this.#request.get(url);
}
};
// src/endpoints/User.ts
var UserEndpoint = class extends BaseEndpoint {
#request = this.createRequest();
get(arg1, query) {
const arg1IsNotObj = arg1 && typeof arg1 !== "object";
const url = arg1IsNotObj ? `users/${arg1}` : "users";
return this.#request.get(url, arg1IsNotObj ? query : arg1);
}
search(keyword, query) {
return this.#request.get("users/search", { q: keyword, ...query || {} });
}
filter(key, value, query) {
return this.#request.get("users/filter", { key, value, ...query || {} });
}
carts(id) {
return this.#request.get(`users/${id}/carts`);
}
posts(id) {
return this.#request.get(`users/${id}/posts`);
}
todos(id) {
return this.#request.get(`users/${id}/todos`);
}
};
// src/DummyJson.ts
var DummyJson = class _DummyJson {
static #instance;
#config;
baseURL = "https://dummyjson.com";
auth = new AuthEndpoint(this);
carts = new CartEndpoint(this);
comments = new CommentEndpoint(this);
posts = new PostEndpoint(this);
products = new ProductEndpoint(this);
quotes = new QuoteEndpoint(this);
recipes = new RecipeEndpoint(this);
todos = new TodoEndpoint(this);
users = new UserEndpoint(this);
constructor(config) {
this.#config = config;
if (_DummyJson.#instance) {
return _DummyJson.#instance;
}
_DummyJson.#instance = this;
}
static get instance() {
return _DummyJson.#instance;
}
setAccessToken(accessToken) {
if (!this.#config) {
this.#config = {};
}
this.#config.Authorization = accessToken;
}
setRefreshToken(refreshToken) {
if (!this.#config) {
this.#config = {};
}
this.#config.refreshToken = refreshToken;
}
get config() {
return this.#config;
}
};
export {
DummyJson
};
//# sourceMappingURL=index.js.map