UNPKG

2.69 kBJavaScriptView Raw
1//@ts-check
2import fetch from "@36node/fetch";
3
4export default class SDK {
5 /**@type {string} **/
6 base;
7 /**@type {string} **/
8 token;
9
10 /**
11 * Sdk auth
12 *
13 * @returns {string} auth header
14 * */
15 get auth() {
16 return "";
17 }
18
19 /**
20 * Init store sdk
21 *
22 * @param {Object} opt
23 * @param {string} opt.base base url
24 * @param {string} opt.token token for authorization
25 */
26 constructor(opt = { base: "", token: "" }) {
27 this.base = opt.base;
28 this.token = opt.token;
29 }
30
31 /**
32 * pet's methods
33 */
34 pet = {
35 /**
36 * List all pets
37 *
38 * @param {ListPetsRequest} req listPets request
39 * @returns {Promise<ListPetsResponse>} A paged array of pets
40 */
41 listPets: req => {
42 const { query } = req || {};
43
44 return fetch(`${this.base}/pets`, {
45 method: "GET",
46 query,
47 headers: { Authorization: this.auth },
48 });
49 },
50 /**
51 * Create a pet
52 *
53 * @param {CreatePetRequest} req createPet request
54 * @returns {Promise<CreatePetResponse>} The Pet created
55 */
56 createPet: req => {
57 const { body } = req || {};
58
59 if (!body) throw new Error("requetBody is required for createPet");
60
61 return fetch(`${this.base}/pets`, {
62 method: "POST",
63 body,
64 headers: { Authorization: this.auth },
65 });
66 },
67 /**
68 * Find pet by id
69 *
70 * @param {ShowPetByIdRequest} req showPetById request
71 * @returns {Promise<ShowPetByIdResponse>} Expected response to a valid request
72 */
73 showPetById: req => {
74 const { petId } = req || {};
75
76 if (!petId) throw new Error("petId is required for showPetById");
77
78 return fetch(`${this.base}/pets/${petId}`, {
79 method: "GET",
80 headers: { Authorization: this.auth },
81 });
82 },
83 /**
84 * Update pet
85 *
86 * @param {UpdatePetRequest} req updatePet request
87 * @returns {Promise<UpdatePetResponse>} The pet
88 */
89 updatePet: req => {
90 const { petId, body } = req || {};
91
92 if (!petId) throw new Error("petId is required for updatePet");
93 if (!body) throw new Error("requetBody is required for updatePet");
94
95 return fetch(`${this.base}/pets/${petId}`, {
96 method: "PUT",
97 body,
98 headers: { Authorization: this.auth },
99 });
100 },
101 /**
102 *
103 *
104 * @param {DeletePetRequest} req deletePet request
105 */
106 deletePet: req => {
107 const { petId } = req || {};
108
109 if (!petId) throw new Error("petId is required for deletePet");
110
111 return fetch(`${this.base}/pets/${petId}`, {
112 method: "DELETE",
113 headers: { Authorization: this.auth },
114 });
115 },
116 };
117}