@ercioko/meblotex-api 0.1.5 → 0.1.7

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/dist/api/Api.d.ts CHANGED
@@ -1,17 +1,16 @@
1
1
  import { DataRow, Order } from '../app_types';
2
2
  import { createApi } from './createApi';
3
- export type RequestOptions<DataType = {
4
- [key: string]: string;
5
- }> = {
3
+ type QueryType<T> = {
4
+ page?: number;
5
+ records?: number;
6
+ fields?: (keyof T)[];
7
+ order?: Order[];
8
+ } & Record<string, unknown>;
9
+ export type RequestOptions<T> = {
6
10
  token?: string;
7
11
  id?: number | string;
8
12
  noId?: boolean;
9
- query?: {
10
- page?: number;
11
- records?: number;
12
- fields?: (keyof DataType)[];
13
- order?: Order[];
14
- } & Record<string, unknown>;
13
+ query?: QueryType<T>;
15
14
  };
16
15
  export type RequestData = Record<string, unknown>;
17
16
  export type ApiResponse<RowType = DataRow> = {
@@ -23,9 +22,12 @@ export default class Api {
23
22
  host: string;
24
23
  namespace: string;
25
24
  endpoint: string;
26
- constructor(endpoint: string, host: string);
25
+ private endpointUrl;
26
+ constructor(endpoint: string, host?: string);
27
27
  get<DataType>(opts?: RequestOptions<DataType extends any[] ? DataType[0] : DataType>): Promise<any>;
28
- post<T, ResponseType>(data?: T, opts?: RequestOptions): Promise<ResponseType>;
29
- put(id: number, data: RequestData, opts?: RequestOptions): Promise<ApiResponse>;
30
- delete(id: number, opts?: RequestOptions): Promise<ApiResponse>;
28
+ post<T, ResponseType>(data?: T, opts?: RequestOptions<T>): Promise<ResponseType>;
29
+ put(id: number, data: RequestData, opts?: RequestOptions<{}>): Promise<ApiResponse>;
30
+ delete(id: number, opts?: RequestOptions<{}>): Promise<ApiResponse>;
31
+ private getQuery;
31
32
  }
33
+ export {};
package/dist/api/Api.js CHANGED
@@ -68,6 +68,7 @@ var Api = /** @class */ (function () {
68
68
  this.host = host;
69
69
  this.namespace = '';
70
70
  this.endpoint = endpoint;
71
+ this.endpointUrl = "".concat(this.host).concat(this.namespace, "/").concat(this.endpoint);
71
72
  }
72
73
  Api.prototype.get = function (opts) {
73
74
  if (opts === void 0) { opts = {}; }
@@ -81,11 +82,8 @@ var Api = /** @class */ (function () {
81
82
  credentials: 'include',
82
83
  headers: headers,
83
84
  };
84
- url = new URL("".concat(this.host).concat(this.namespace, "/").concat(this.endpoint, "/").concat(opts.id || ''));
85
- url.search = opts.query
86
- ? new URLSearchParams(utils_1.Query.encode(opts.query)).toString()
87
- : '';
88
- return [2 /*return*/, fetch(url.href, options).then(handleResponse)];
85
+ url = "".concat(this.endpointUrl, "/").concat(opts.id || '').concat(this.getQuery(opts.query));
86
+ return [2 /*return*/, fetch(url, options).then(handleResponse)];
89
87
  });
90
88
  });
91
89
  };
@@ -99,11 +97,8 @@ var Api = /** @class */ (function () {
99
97
  headers.append('Content-Type', 'application/json');
100
98
  if (opts.token)
101
99
  headers.append('Authorization', "Bearer ".concat(opts.token));
102
- url = new URL("".concat(this.host).concat(this.namespace, "/").concat(this.endpoint));
103
- url.search = opts.query
104
- ? new URLSearchParams(utils_1.Query.encode(opts.query)).toString()
105
- : '';
106
- return [2 /*return*/, fetch(url.href, {
100
+ url = "".concat(this.endpointUrl).concat(this.getQuery(opts.query));
101
+ return [2 /*return*/, fetch(url, {
107
102
  method: 'POST',
108
103
  body: body,
109
104
  headers: headers,
@@ -122,11 +117,8 @@ var Api = /** @class */ (function () {
122
117
  headers.append('Content-Type', 'application/json');
123
118
  if (opts.token)
124
119
  headers.append('Authorization', "Bearer ".concat(opts.token));
125
- url = new URL("".concat(this.host).concat(this.namespace, "/").concat(this.endpoint).concat(opts.noId ? '' : '/').concat(id || ''));
126
- url.search = opts.query
127
- ? new URLSearchParams(utils_1.Query.encode(opts.query)).toString()
128
- : '';
129
- return [2 /*return*/, fetch(url.href, {
120
+ url = "".concat(this.endpointUrl).concat(opts.noId ? '' : '/').concat(id || '').concat(this.getQuery(opts.query));
121
+ return [2 /*return*/, fetch(url, {
130
122
  method: 'PUT',
131
123
  body: body,
132
124
  headers: headers,
@@ -143,11 +135,8 @@ var Api = /** @class */ (function () {
143
135
  headers = new Headers();
144
136
  if (opts.token)
145
137
  headers.append('Authorization', "Bearer ".concat(opts.token));
146
- url = new URL("".concat(this.host).concat(this.namespace, "/").concat(this.endpoint, "/").concat(id));
147
- url.search = opts.query
148
- ? new URLSearchParams(utils_1.Query.encode(opts.query)).toString()
149
- : '';
150
- return [2 /*return*/, fetch(url.href, {
138
+ url = "".concat(this.endpointUrl, "/").concat(id).concat(this.getQuery(opts.query));
139
+ return [2 /*return*/, fetch(url, {
151
140
  headers: headers,
152
141
  method: 'DELETE',
153
142
  credentials: 'include',
@@ -155,6 +144,9 @@ var Api = /** @class */ (function () {
155
144
  });
156
145
  });
157
146
  };
147
+ Api.prototype.getQuery = function (query) {
148
+ return query ? "?".concat(new URLSearchParams(utils_1.Query.encode(query)).toString()) : '';
149
+ };
158
150
  return Api;
159
151
  }());
160
152
  exports.default = Api;
@@ -3,11 +3,11 @@ type CallbackFunction<T> = (reason: unknown) => any;
3
3
  export default class Endpoint<GetResponseElement> extends Api {
4
4
  endpoint: string;
5
5
  errorCallback?: CallbackFunction<any>;
6
- constructor(endpoint: string, host: string, errorCallback?: (reason: unknown) => ApiResponse<any>);
6
+ constructor(endpoint: string, host?: string, errorCallback?: (reason: unknown) => ApiResponse<any>);
7
7
  get<DataType = GetResponseElement>(opts?: RequestOptions<DataType extends any[] ? DataType[0] : DataType>): Promise<DataType extends any[] ? ApiResponse<DataType> : DataType>;
8
- post<T = RequestData, ResponseType = T>(data?: T, opts?: RequestOptions): Promise<ResponseType>;
9
- put(id: number, data: RequestData, opts?: RequestOptions): Promise<ApiResponse>;
10
- delete(id: number, opts?: RequestOptions): Promise<ApiResponse>;
8
+ post<T = RequestData, ResponseType = T>(data?: T, opts?: RequestOptions<T>): Promise<ResponseType>;
9
+ put(id: number, data: RequestData, opts?: RequestOptions<{}>): Promise<ApiResponse>;
10
+ delete(id: number, opts?: RequestOptions<{}>): Promise<ApiResponse>;
11
11
  toJSON(proto: Record<string, unknown>): Record<string, unknown>;
12
12
  }
13
13
  export {};
@@ -58,6 +58,7 @@ var Api_1 = __importDefault(require("./Api"));
58
58
  var Endpoint = /** @class */ (function (_super) {
59
59
  __extends(Endpoint, _super);
60
60
  function Endpoint(endpoint, host, errorCallback) {
61
+ if (host === void 0) { host = ''; }
61
62
  var _this = _super.call(this, endpoint, host) || this;
62
63
  _this.endpoint = endpoint;
63
64
  _this.errorCallback = errorCallback;
@@ -25,7 +25,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.useApiHost = exports.ApiHostProvider = void 0;
27
27
  var react_1 = __importStar(require("react"));
28
- var ApiHostContext = (0, react_1.createContext)('/api');
28
+ var ApiHostContext = (0, react_1.createContext)('');
29
29
  var ApiHostProvider = function (_a) {
30
30
  var children = _a.children, host = _a.host;
31
31
  return (react_1.default.createElement(ApiHostContext.Provider, { value: host }, children));
@@ -199,9 +199,11 @@ var Api = /*#__PURE__*/ function() {
199
199
  _define_property(this, "host", void 0);
200
200
  _define_property(this, "namespace", void 0);
201
201
  _define_property(this, "endpoint", void 0);
202
+ _define_property(this, "endpointUrl", void 0);
202
203
  this.host = host;
203
204
  this.namespace = '';
204
205
  this.endpoint = endpoint;
206
+ this.endpointUrl = "".concat(this.host).concat(this.namespace, "/").concat(this.endpoint);
205
207
  }
206
208
  _create_class(Api, [
207
209
  {
@@ -218,11 +220,10 @@ var Api = /*#__PURE__*/ function() {
218
220
  credentials: 'include',
219
221
  headers: headers
220
222
  };
221
- url = new URL("".concat(_this.host).concat(_this.namespace, "/").concat(_this.endpoint, "/").concat(opts.id || ''));
222
- url.search = opts.query ? new URLSearchParams(Query.encode(opts.query)).toString() : '';
223
+ url = "".concat(_this.endpointUrl, "/").concat(opts.id || '').concat(_this.getQuery(opts.query));
223
224
  return [
224
225
  2,
225
- fetch(url.href, options).then(handleResponse)
226
+ fetch(url, options).then(handleResponse)
226
227
  ];
227
228
  });
228
229
  })();
@@ -240,11 +241,10 @@ var Api = /*#__PURE__*/ function() {
240
241
  body = JSON.stringify(data);
241
242
  headers.append('Content-Type', 'application/json');
242
243
  if (opts.token) headers.append('Authorization', "Bearer ".concat(opts.token));
243
- url = new URL("".concat(_this.host).concat(_this.namespace, "/").concat(_this.endpoint));
244
- url.search = opts.query ? new URLSearchParams(Query.encode(opts.query)).toString() : '';
244
+ url = "".concat(_this.endpointUrl).concat(_this.getQuery(opts.query));
245
245
  return [
246
246
  2,
247
- fetch(url.href, {
247
+ fetch(url, {
248
248
  method: 'POST',
249
249
  body: body,
250
250
  headers: headers,
@@ -267,11 +267,10 @@ var Api = /*#__PURE__*/ function() {
267
267
  headers = new Headers();
268
268
  headers.append('Content-Type', 'application/json');
269
269
  if (opts.token) headers.append('Authorization', "Bearer ".concat(opts.token));
270
- url = new URL("".concat(_this.host).concat(_this.namespace, "/").concat(_this.endpoint).concat(opts.noId ? '' : '/').concat(id || ''));
271
- url.search = opts.query ? new URLSearchParams(Query.encode(opts.query)).toString() : '';
270
+ url = "".concat(_this.endpointUrl).concat(opts.noId ? '' : '/').concat(id || '').concat(_this.getQuery(opts.query));
272
271
  return [
273
272
  2,
274
- fetch(url.href, {
273
+ fetch(url, {
275
274
  method: 'PUT',
276
275
  body: body,
277
276
  headers: headers,
@@ -292,11 +291,10 @@ var Api = /*#__PURE__*/ function() {
292
291
  return _ts_generator(this, function(_state) {
293
292
  headers = new Headers();
294
293
  if (opts.token) headers.append('Authorization', "Bearer ".concat(opts.token));
295
- url = new URL("".concat(_this.host).concat(_this.namespace, "/").concat(_this.endpoint, "/").concat(id));
296
- url.search = opts.query ? new URLSearchParams(Query.encode(opts.query)).toString() : '';
294
+ url = "".concat(_this.endpointUrl, "/").concat(id).concat(_this.getQuery(opts.query));
297
295
  return [
298
296
  2,
299
- fetch(url.href, {
297
+ fetch(url, {
300
298
  headers: headers,
301
299
  method: 'DELETE',
302
300
  credentials: 'include'
@@ -305,6 +303,12 @@ var Api = /*#__PURE__*/ function() {
305
303
  });
306
304
  })();
307
305
  }
306
+ },
307
+ {
308
+ key: "getQuery",
309
+ value: function getQuery(query) {
310
+ return query ? "?".concat(new URLSearchParams(Query.encode(query)).toString()) : '';
311
+ }
308
312
  }
309
313
  ]);
310
314
  return Api;
@@ -248,7 +248,8 @@ var Endpoint = /*#__PURE__*/ function(Api) {
248
248
  "use strict";
249
249
  _inherits(Endpoint, Api);
250
250
  var _super = _create_super(Endpoint);
251
- function Endpoint(endpoint, host, errorCallback) {
251
+ function Endpoint(endpoint) {
252
+ var host = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : '', errorCallback = arguments.length > 2 ? arguments[2] : void 0;
252
253
  _class_call_check(this, Endpoint);
253
254
  var _this;
254
255
  _this = _super.call(this, endpoint, host);
@@ -1,5 +1,5 @@
1
1
  import React, { createContext, useContext } from 'react';
2
- var ApiHostContext = /*#__PURE__*/ createContext('/api');
2
+ var ApiHostContext = /*#__PURE__*/ createContext('');
3
3
  export var ApiHostProvider = function(param) {
4
4
  var children = param.children, host = param.host;
5
5
  return /*#__PURE__*/ React.createElement(ApiHostContext.Provider, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ercioko/meblotex-api",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "Shared modules for Meblotex app",
5
5
  "main": "./dist/index.js",
6
6
  "typings": "./dist/index.d.ts",