kodzero-front-sdk-alfa 0.0.2 → 0.0.5

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.
Files changed (82) hide show
  1. package/README.md +28 -11
  2. package/dist/Kodzero.d.ts +3 -2
  3. package/dist/Kodzero.js +9 -5
  4. package/dist/__mocks__/fluid-fetch.d.ts +1 -0
  5. package/dist/__mocks__/fluid-fetch.js +23 -0
  6. package/dist/__mocks__/validno.d.ts +5 -0
  7. package/dist/__mocks__/validno.js +9 -0
  8. package/dist/auth/base.d.ts +3 -2
  9. package/dist/auth/base.js +10 -6
  10. package/dist/auth/email.d.ts +2 -2
  11. package/dist/auth/email.js +64 -62
  12. package/dist/auth/index.d.ts +4 -3
  13. package/dist/auth/index.js +6 -2
  14. package/dist/auth/tokens.d.ts +2 -1
  15. package/dist/auth/tokens.js +4 -1
  16. package/dist/errors/KodzeroApiError.js +3 -0
  17. package/dist/errors/KodzeroValidationError.js +1 -0
  18. package/dist/index.d.ts +2 -1
  19. package/dist/index.js +1 -48
  20. package/dist/model/BaseModel.d.ts +3 -3
  21. package/dist/model/BaseModel.js +26 -28
  22. package/dist/model/PaginatedResult.d.ts +28 -0
  23. package/dist/model/PaginatedResult.js +49 -0
  24. package/dist/model/createModel.d.ts +3 -1
  25. package/dist/model/createModel.js +146 -148
  26. package/dist/schemas/baseAuth.js +13 -13
  27. package/dist/schemas/baseModel.js +20 -20
  28. package/dist/tsconfig.tsbuildinfo +1 -1
  29. package/dist/utils/buildURL.js +3 -1
  30. package/dist/utils/validateApiResponse.js +1 -1
  31. package/jest.config.ts +23 -19
  32. package/package.json +7 -6
  33. package/src/Kodzero.ts +2 -2
  34. package/src/__mocks__/fluid-fetch.ts +35 -0
  35. package/src/__mocks__/validno.ts +12 -0
  36. package/src/__tests__/Kodzero.test.ts +9 -0
  37. package/src/__tests__/utils/buildURL.test.ts +18 -0
  38. package/src/auth/base.ts +5 -3
  39. package/src/auth/email.ts +3 -3
  40. package/src/auth/index.ts +6 -4
  41. package/src/auth/tokens.ts +4 -2
  42. package/src/index.ts +3 -0
  43. package/src/model/BaseModel.ts +11 -14
  44. package/src/model/PaginatedResult.ts +81 -0
  45. package/src/model/createModel.ts +40 -16
  46. package/src/schemas/baseAuth.ts +2 -1
  47. package/src/schemas/baseModel.ts +1 -1
  48. package/src/tsconfig.json +9 -8
  49. package/src/utils/{buildURL_rename.ts → buildURL.ts} +2 -1
  50. package/dist/fetcher/InterceptorManager.d.ts +0 -9
  51. package/dist/fetcher/InterceptorManager.js +0 -42
  52. package/dist/fetcher/MiddlewareManager.d.ts +0 -9
  53. package/dist/fetcher/MiddlewareManager.js +0 -42
  54. package/dist/fetcher/index.d.ts +0 -77
  55. package/dist/fetcher/index.js +0 -194
  56. package/dist/model/baseModelOptionsSchema.d.ts +0 -2
  57. package/dist/model/baseModelOptionsSchema.js +0 -10
  58. package/dist/model/configSchema.d.ts +0 -0
  59. package/dist/model/configSchema.js +0 -1
  60. package/dist/model/errors/KodzeroApiError.d.ts +0 -7
  61. package/dist/model/errors/KodzeroApiError.js +0 -10
  62. package/dist/model/modelFactory.d.ts +0 -50
  63. package/dist/model/modelFactory.js +0 -41
  64. package/dist/model/modelSchema.d.ts +0 -0
  65. package/dist/model/modelSchema.js +0 -1
  66. package/dist/model/schemas/baseModel.d.ts +0 -6
  67. package/dist/model/schemas/baseModel.js +0 -25
  68. package/dist/model/schemas/baseModelOptionsSchema.d.ts +0 -2
  69. package/dist/model/schemas/baseModelOptionsSchema.js +0 -10
  70. package/dist/model/statics/getAll.d.ts +0 -2
  71. package/dist/model/statics/getAll.js +0 -4
  72. package/dist/model/utils/processUrl.d.ts +0 -2
  73. package/dist/model/utils/processUrl.js +0 -7
  74. package/dist/model/utils/validateApiResponse.d.ts +0 -2
  75. package/dist/model/utils/validateApiResponse.js +0 -14
  76. package/dist/schemas/baseModel copy.d.ts +0 -6
  77. package/dist/schemas/baseModel copy.js +0 -25
  78. package/dist/utils/buildURL_rename.d.ts +0 -2
  79. package/dist/utils/buildURL_rename.js +0 -7
  80. package/dist/utils/processUrl.d.ts +0 -2
  81. package/dist/utils/processUrl.js +0 -7
  82. /package/{.eslintrc.cjs → eslintrc.cjs} +0 -0
package/README.md CHANGED
@@ -7,7 +7,6 @@ A lightweight OOP library for frontend applications to easily interact with Kodz
7
7
  ```bash
8
8
  npm install kodzero-front-sdk-alfa
9
9
  ```
10
- ```
11
10
 
12
11
  ## Authentication
13
12
 
@@ -54,8 +53,6 @@ Creating and using models is the core functionality of the SDK. Models provide a
54
53
  ### Creating a Model
55
54
 
56
55
  ```javascript
57
- import Schema from 'validno';
58
-
59
56
  // Define your data model interface
60
57
  interface User {
61
58
  _id?: string;
@@ -64,13 +61,13 @@ interface User {
64
61
  createdAt?: Date;
65
62
  }
66
63
 
67
- // Optional: Create a schema for validation
68
- const userSchema = new Schema({
64
+ // Optional: Create a schema object for validation
65
+ const userSchema = {
69
66
  _id: { type: String },
70
67
  name: { type: String },
71
68
  email: { type: String },
72
69
  createdAt: { type: Date }
73
- });
70
+ };
74
71
 
75
72
  // Create a model for the 'users' collection
76
73
  const User = kodzero.createModel<User>({
@@ -142,6 +139,14 @@ interface CarMethods {
142
139
  isVintage: () => boolean;
143
140
  }
144
141
 
142
+ // Define your schema
143
+ const carSchema = {
144
+ _id: { type: String },
145
+ make: { type: String },
146
+ model: { type: String },
147
+ year: { type: Number }
148
+ };
149
+
145
150
  // Create the model with the custom methods type
146
151
  const Car = kodzero.createModel<Car, CarMethods>({
147
152
  collection: 'cars',
@@ -224,16 +229,16 @@ const deleteResults = await User.deleteMany(['id1', 'id2', 'id3']);
224
229
 
225
230
  ## Validation
226
231
 
227
- The SDK uses the `validno` library for schema-based validation. When creating a model with a schema, you can validate your data:
232
+ The SDK has built-in validation capabilities based on `validno` package ([Validno docs](https://validno.kodzero.pro/)). When creating a model with a schema, you can validate your data:
228
233
 
229
234
  ```javascript
230
235
  // Define a schema with validation rules
231
- const carSchema = new Schema({
236
+ const carSchema = {
232
237
  _id: { type: String },
233
238
  make: { type: String, required: true },
234
239
  model: { type: String, required: true },
235
- year: { type: Number, min: 1900, max: 2100 }
236
- });
240
+ year: { type: Number }
241
+ };
237
242
 
238
243
  const Car = kodzero.createModel<Car>({
239
244
  collection: 'cars',
@@ -269,8 +274,20 @@ interface Profile {
269
274
  }
270
275
  }
271
276
 
277
+ const profileSchema = {
278
+ _id: { type: String },
279
+ user: {
280
+ name: { type: String },
281
+ contact: {
282
+ email: { type: String },
283
+ phone: { type: String }
284
+ }
285
+ }
286
+ };
287
+
272
288
  const Profile = kodzero.createModel<Profile>({
273
- collection: 'profiles'
289
+ collection: 'profiles',
290
+ schema: profileSchema
274
291
  });
275
292
 
276
293
  const profile = new Profile({
package/dist/Kodzero.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import FluidFetch from "fluid-fetch";
2
- import { KodzeroAuth } from "./auth/index.js";
2
+ import KodzeroAuth from "./auth/index.js";
3
3
  import { ModelOptions } from "./model/BaseModel.js";
4
- import { TokensManagerClass } from "./auth/tokens.js";
4
+ import TokensManagerClass from "./auth/tokens.js";
5
5
  interface Options {
6
6
  host: string;
7
7
  }
@@ -19,6 +19,7 @@ declare class Kodzero {
19
19
  registerMethod<K extends keyof M>(name: K, fn: M[K]): void;
20
20
  find(id: string): Promise<T>;
21
21
  findMany(options?: import("./model/createModel.js").FindManyOptions | {}): Promise<T[]>;
22
+ findManyPaginated(options?: import("./model/createModel.js").FindManyOptions | {}, page?: number, perPage?: number): Promise<import("./model/PaginatedResult.js").default<T>>;
22
23
  create(data: T): Promise<T>;
23
24
  createMany(data: T[]): Promise<T[]>;
24
25
  update(id: string, data: Partial<T>): Promise<T>;
package/dist/Kodzero.js CHANGED
@@ -1,12 +1,13 @@
1
1
  import FluidFetch from "fluid-fetch";
2
- import { KodzeroAuth } from "./auth/index.js";
2
+ import KodzeroAuth from "./auth/index.js";
3
3
  import createModel from "./model/createModel.js";
4
- import { TokensManagerClass } from "./auth/tokens.js";
4
+ import TokensManagerClass from "./auth/tokens.js";
5
5
  class Kodzero {
6
+ host;
7
+ auth;
8
+ tokensManager;
9
+ api;
6
10
  constructor(options) {
7
- this.createModel = (options) => {
8
- return createModel(Object.assign(Object.assign({}, options), { host: this.host }), this.api);
9
- };
10
11
  this.tokensManager = new TokensManagerClass('', '');
11
12
  this.host = options.host;
12
13
  this.api = new FluidFetch();
@@ -18,5 +19,8 @@ class Kodzero {
18
19
  return req;
19
20
  });
20
21
  }
22
+ createModel = (options) => {
23
+ return createModel({ ...options, host: this.host }, this.api);
24
+ };
21
25
  }
22
26
  export default Kodzero;
@@ -0,0 +1 @@
1
+ export default function FluidFetch(this: any, options?: {}): void;
@@ -0,0 +1,23 @@
1
+ export default function FluidFetch(options = {}) {
2
+ this.options = options;
3
+ const middlewares = {
4
+ request: {
5
+ use: jest.fn()
6
+ },
7
+ response: {
8
+ use: jest.fn()
9
+ }
10
+ };
11
+ const request = jest.fn().mockImplementation(() => Promise.resolve({
12
+ data: {},
13
+ headers: {},
14
+ status: 200
15
+ }));
16
+ this.middlewares = middlewares;
17
+ this.request = request;
18
+ this.api = {
19
+ middlewares,
20
+ request
21
+ };
22
+ }
23
+ FluidFetch.prototype.constructor = FluidFetch;
@@ -0,0 +1,5 @@
1
+ export default function Schema(): {
2
+ validate: () => {
3
+ ok: boolean;
4
+ };
5
+ };
@@ -0,0 +1,9 @@
1
+ export default function Schema() {
2
+ return {
3
+ validate: () => {
4
+ return {
5
+ ok: true
6
+ };
7
+ },
8
+ };
9
+ }
@@ -1,7 +1,7 @@
1
1
  import FluidFetch from "fluid-fetch";
2
2
  import { AuthOptions } from "./index.js";
3
- import { TokensManagerClass } from "./tokens.js";
4
- export declare class KodzeroAuthBase {
3
+ import TokensManagerClass from "./tokens.js";
4
+ declare class KodzeroAuthBase {
5
5
  host: string;
6
6
  api: typeof FluidFetch;
7
7
  tokensManager: TokensManagerClass;
@@ -13,3 +13,4 @@ export declare class KodzeroAuthBase {
13
13
  signout: (...args: any[]) => Promise<any> | void;
14
14
  verify: (...args: any[]) => Promise<any> | void;
15
15
  }
16
+ export default KodzeroAuthBase;
package/dist/auth/base.js CHANGED
@@ -1,12 +1,10 @@
1
1
  import BaseAuthSchema from "../schemas/baseAuth.js";
2
2
  import validateApiResponse from "../utils/validateApiResponse.js";
3
- export class KodzeroAuthBase {
3
+ class KodzeroAuthBase {
4
+ host;
5
+ api;
6
+ tokensManager;
4
7
  constructor(options, api, tokensManager) {
5
- this.signin = (...args) => { };
6
- this.signup = (...args) => { };
7
- this.refresh = (...args) => { };
8
- this.signout = (...args) => { };
9
- this.verify = (...args) => { };
10
8
  BaseAuthSchema.validateOrThrow(options);
11
9
  this.host = options.host;
12
10
  this.api = api;
@@ -15,4 +13,10 @@ export class KodzeroAuthBase {
15
13
  _handleApiError(response) {
16
14
  return validateApiResponse(response);
17
15
  }
16
+ signin = (...args) => { };
17
+ signup = (...args) => { };
18
+ refresh = (...args) => { };
19
+ signout = (...args) => { };
20
+ verify = (...args) => { };
18
21
  }
22
+ export default KodzeroAuthBase;
@@ -1,7 +1,7 @@
1
1
  import FluidFetch from "fluid-fetch";
2
2
  import { AuthOptions } from "./index.js";
3
- import { KodzeroAuthBase } from "./base.js";
4
- import { TokensManagerClass } from "./tokens.js";
3
+ import KodzeroAuthBase from "./base.js";
4
+ import TokensManagerClass from "./tokens.js";
5
5
  interface KodzeroAuthEmailSignin {
6
6
  email: string;
7
7
  password: string;
@@ -1,70 +1,72 @@
1
- import { KodzeroAuthBase } from "./base.js";
2
- import buildURL from "../utils/buildURL_rename.js";
1
+ import buildURL from "../utils/buildURL.js";
2
+ import KodzeroAuthBase from "./base.js";
3
3
  class KodzeroAuthEmail extends KodzeroAuthBase {
4
+ tokensManager;
5
+ collection;
4
6
  constructor(options, api, tokensManager) {
5
7
  super(options, api, tokensManager);
6
- this._setTokens = (access, refresh) => {
7
- this.tokensManager.setAccess(access);
8
- if (refresh)
9
- this.tokensManager.setRefresh(refresh);
10
- };
11
- this.signin = async (input) => {
12
- const url = buildURL(this.host, this.collection + '/signin');
13
- const response = await this.api.post(url, input)
14
- .headers({ 'Content-Type': 'application/json' });
15
- await this._handleApiError(response);
16
- const json = await response.json();
17
- if (json.ok && json.tokens && json.tokens.access && json.tokens.refresh) {
18
- this._setTokens(json.tokens.access, json.tokens.refresh);
19
- }
20
- return json.tokens;
21
- };
22
- this.signup = async (userData) => {
23
- const url = buildURL(this.host, this.collection + '/signup');
24
- const response = await this.api.post(url, userData)
25
- .headers({ 'Content-Type': 'application/json' });
26
- await this._handleApiError(response);
27
- const json = await response.json();
28
- if (json.ok && json.result && json.result.tokens.access && json.result.tokens.refresh) {
29
- this._setTokens(json.result.tokens.access, json.result.tokens.refresh);
30
- }
31
- return json.result.user;
32
- };
33
- this.verify = async () => {
34
- try {
35
- const url = buildURL(this.host, this.collection + '/verify');
36
- const response = await this.api.get(url);
37
- const json = await response.json();
38
- return json.ok ? true : false;
39
- }
40
- catch (error) {
41
- console.warn("Token verification error:", error);
42
- return false;
43
- }
44
- };
45
- this.refresh = async () => {
46
- const url = buildURL(this.host, this.collection + '/refresh');
47
- const response = await this.api.post(url, { refresh: this.tokensManager.refresh })
48
- .headers({ 'Content-Type': 'application/json' });
49
- await this._handleApiError(response);
50
- const json = await response.json();
51
- if (json.ok && json.tokens && json.tokens.access) {
52
- this.tokensManager.setAccess(json.tokens.access);
53
- }
54
- return json.ok ? true : false;
55
- };
56
- this.signout = async () => {
57
- const url = buildURL(this.host, this.collection + '/signout');
58
- const response = await this.api.post(url, {})
59
- .headers({ 'Content-Type': 'application/json' });
60
- await this._handleApiError(response);
61
- const json = await response.json();
62
- if (json.ok)
63
- this.tokensManager.clear();
64
- return json.ok ? true : false;
65
- };
66
8
  this.tokensManager = tokensManager;
67
9
  this.collection = "auth/password";
68
10
  }
11
+ _setTokens = (access, refresh) => {
12
+ this.tokensManager.setAccess(access);
13
+ if (refresh)
14
+ this.tokensManager.setRefresh(refresh);
15
+ };
16
+ signin = async (input) => {
17
+ const url = buildURL(this.host, this.collection + '/signin');
18
+ const response = await this.api.post(url, input)
19
+ .headers({ 'Content-Type': 'application/json' });
20
+ await this._handleApiError(response);
21
+ const json = await response.json();
22
+ if (json.ok && json.tokens && json.tokens.access && json.tokens.refresh) {
23
+ this._setTokens(json.tokens.access, json.tokens.refresh);
24
+ }
25
+ return json.tokens;
26
+ };
27
+ signup = async (userData) => {
28
+ const url = buildURL(this.host, this.collection + '/signup');
29
+ const response = await this.api.post(url, userData)
30
+ .headers({ 'Content-Type': 'application/json' });
31
+ await this._handleApiError(response);
32
+ const json = await response.json();
33
+ if (json.ok && json.result && json.result.tokens.access && json.result.tokens.refresh) {
34
+ this._setTokens(json.result.tokens.access, json.result.tokens.refresh);
35
+ }
36
+ return json.result.user;
37
+ };
38
+ verify = async () => {
39
+ try {
40
+ const url = buildURL(this.host, this.collection + '/verify');
41
+ const response = await this.api.get(url);
42
+ const json = await response.json();
43
+ return json.ok ? true : false;
44
+ }
45
+ catch (error) {
46
+ console.warn("Token verification error:", error);
47
+ return false;
48
+ }
49
+ };
50
+ refresh = async () => {
51
+ const url = buildURL(this.host, this.collection + '/refresh');
52
+ const response = await this.api.post(url, { refresh: this.tokensManager.refresh })
53
+ .headers({ 'Content-Type': 'application/json' });
54
+ await this._handleApiError(response);
55
+ const json = await response.json();
56
+ if (json.ok && json.tokens && json.tokens.access) {
57
+ this.tokensManager.setAccess(json.tokens.access);
58
+ }
59
+ return json.ok ? true : false;
60
+ };
61
+ signout = async () => {
62
+ const url = buildURL(this.host, this.collection + '/signout');
63
+ const response = await this.api.post(url, {})
64
+ .headers({ 'Content-Type': 'application/json' });
65
+ await this._handleApiError(response);
66
+ const json = await response.json();
67
+ if (json.ok)
68
+ this.tokensManager.clear();
69
+ return json.ok ? true : false;
70
+ };
69
71
  }
70
72
  export default KodzeroAuthEmail;
@@ -1,13 +1,14 @@
1
1
  import FluidFetch from "fluid-fetch";
2
- import { KodzeroAuthBase } from "./base.js";
2
+ import KodzeroAuthBase from "./base.js";
3
3
  import KodzeroAuthEmail from "./email.js";
4
- import { TokensManagerClass } from "./tokens.js";
4
+ import TokensManagerClass from "./tokens.js";
5
5
  export interface AuthOptions {
6
6
  host: string;
7
7
  }
8
- export declare class KodzeroAuth extends KodzeroAuthBase {
8
+ declare class KodzeroAuth extends KodzeroAuthBase {
9
9
  email: KodzeroAuthEmail;
10
10
  setTokens: (access: string, refresh?: string) => void;
11
11
  clearTokens: () => void;
12
12
  constructor(options: AuthOptions, api: typeof FluidFetch, tokensManager: TokensManagerClass);
13
13
  }
14
+ export default KodzeroAuth;
@@ -1,6 +1,9 @@
1
- import { KodzeroAuthBase } from "./base.js";
1
+ import KodzeroAuthBase from "./base.js";
2
2
  import KodzeroAuthEmail from "./email.js";
3
- export class KodzeroAuth extends KodzeroAuthBase {
3
+ class KodzeroAuth extends KodzeroAuthBase {
4
+ email;
5
+ setTokens;
6
+ clearTokens;
4
7
  constructor(options, api, tokensManager) {
5
8
  super(options, api, tokensManager);
6
9
  this.email = new KodzeroAuthEmail(options, api, tokensManager);
@@ -19,3 +22,4 @@ export class KodzeroAuth extends KodzeroAuthBase {
19
22
  };
20
23
  }
21
24
  }
25
+ export default KodzeroAuth;
@@ -1,4 +1,4 @@
1
- export declare class TokensManagerClass {
1
+ declare class TokensManagerClass {
2
2
  access: string;
3
3
  refresh: string;
4
4
  constructor(access?: string, refresh?: string);
@@ -8,3 +8,4 @@ export declare class TokensManagerClass {
8
8
  setRefresh(token: string): void;
9
9
  clear(): void;
10
10
  }
11
+ export default TokensManagerClass;
@@ -1,4 +1,6 @@
1
- export class TokensManagerClass {
1
+ class TokensManagerClass {
2
+ access;
3
+ refresh;
2
4
  constructor(access = '', refresh = '') {
3
5
  this.access = access;
4
6
  this.refresh = refresh;
@@ -20,3 +22,4 @@ export class TokensManagerClass {
20
22
  this.refresh = '';
21
23
  }
22
24
  }
25
+ export default TokensManagerClass;
@@ -1,4 +1,7 @@
1
1
  class KodzeroApiError extends Error {
2
+ url;
3
+ statusCode;
4
+ details;
2
5
  constructor(url, statusCode, message, details) {
3
6
  super(message);
4
7
  this.name = "KodzeroApiError";
@@ -1,4 +1,5 @@
1
1
  class KodzeroValidationError extends Error {
2
+ errors;
2
3
  constructor(message, errors = []) {
3
4
  super(message);
4
5
  this.name = "KodzeroValidationError";
package/dist/index.d.ts CHANGED
@@ -1 +1,2 @@
1
- export {};
1
+ import Kodzero from "./Kodzero.js";
2
+ export default Kodzero;
package/dist/index.js CHANGED
@@ -1,49 +1,2 @@
1
- import Schema from "validno";
2
1
  import Kodzero from "./Kodzero.js";
3
- console.log('front-sdk started');
4
- const kodzero = new Kodzero({
5
- host: 'http://localhost:6969'
6
- });
7
- const carSchema = new Schema({
8
- _id: { type: String },
9
- mark: { type: String },
10
- model: { type: String },
11
- year: { type: Number }
12
- });
13
- const Car = kodzero.createModel({
14
- collection: 'cars',
15
- schema: carSchema
16
- });
17
- Car.registerMethod('greet', function () {
18
- return `Hello, I am a ${this.modelData.mark} ${this.modelData.model} from ${this.modelData.year}`;
19
- });
20
- const carRecord = await Car.get('68aec23b4732a44b3f559250');
21
- const data = carRecord.data();
22
- console.log(carRecord.greet());
23
- const signRes = await kodzero.auth.email.signin({ email: 'leshatour@gmail.com', password: 'qwerty123' });
24
- const verifyRes = await kodzero.auth.email.verify();
25
- console.log('verify1', verifyRes);
26
- const refreshRes = await kodzero.auth.email.refresh();
27
- console.log('refresh', refreshRes);
28
- const logoutRes = await kodzero.auth.email.signout();
29
- console.log('logout', logoutRes);
30
- const verifyRes2 = await kodzero.auth.email.verify();
31
- console.log('verify2', verifyRes2);
32
- const signupRes = await kodzero.auth.email.signup({
33
- "name": "leshatour2",
34
- "email": `leshatour${Math.random()}@gmail.com`,
35
- "password": "qwerty123"
36
- });
37
- console.log('signup', signupRes);
38
- const URec = kodzero.createModel({
39
- collection: 'scope-user',
40
- schema: new Schema({
41
- text: '134'
42
- })
43
- });
44
- console.log(1);
45
- const urec = new URec({ _id: '', text: 'User Scope Example' });
46
- console.log(2);
47
- await urec.save();
48
- console.log(3);
49
- console.log(urec.data()._id);
2
+ export default Kodzero;
@@ -3,7 +3,7 @@ import FluidFetch from "fluid-fetch";
3
3
  export interface ModelOptions {
4
4
  host: string;
5
5
  collection: string;
6
- schema?: typeof Schema;
6
+ schema?: Record<string, any>;
7
7
  }
8
8
  declare class BaseModel<T extends {
9
9
  _id?: string;
@@ -12,12 +12,12 @@ declare class BaseModel<T extends {
12
12
  collection: string;
13
13
  modelData: T;
14
14
  schema?: typeof Schema;
15
- api: typeof FluidFetch;
15
+ apiClient: typeof FluidFetch;
16
16
  id: string | null;
17
17
  static url: string;
18
18
  static collection: string;
19
19
  static api: typeof FluidFetch;
20
- constructor(options: ModelOptions, api: typeof FluidFetch);
20
+ constructor(options: ModelOptions, apiClient: typeof FluidFetch);
21
21
  _setId: (id: string | null) => void;
22
22
  _setModelData: (data: T) => void;
23
23
  _handleApiError(response: Response): Promise<void>;
@@ -1,35 +1,33 @@
1
- var __rest = (this && this.__rest) || function (s, e) {
2
- var t = {};
3
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
- t[p] = s[p];
5
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
- t[p[i]] = s[p[i]];
9
- }
10
- return t;
11
- };
1
+ import Schema from "validno";
12
2
  import constants from "./constants.js";
13
- import validateApiResponse from "../utils/validateApiResponse.js";
3
+ import buildURL from "../utils/buildURL.js";
14
4
  import BaseModelSchema from "../schemas/baseModel.js";
15
- import buildURL from "../utils/buildURL_rename.js";
5
+ import validateApiResponse from "../utils/validateApiResponse.js";
16
6
  class BaseModel {
17
- constructor(options, api) {
18
- this.modelData = {};
19
- this._setId = (id) => {
20
- this.id = id;
21
- };
22
- this._setModelData = (data) => {
23
- this.modelData = data;
24
- this._setId((data === null || data === void 0 ? void 0 : data._id) || null);
25
- };
7
+ host;
8
+ collection;
9
+ modelData = {};
10
+ schema;
11
+ apiClient;
12
+ id;
13
+ static url;
14
+ static collection;
15
+ static api;
16
+ constructor(options, apiClient) {
26
17
  BaseModelSchema.validateOrThrow(options);
27
18
  this.host = options.host;
28
19
  this.collection = options.collection;
29
- this.schema = options.schema;
30
- this.api = api;
20
+ this.schema = options.schema ? new Schema({ ...options.schema }) : null;
21
+ this.apiClient = apiClient;
31
22
  this.id = null;
32
23
  }
24
+ _setId = (id) => {
25
+ this.id = id;
26
+ };
27
+ _setModelData = (data) => {
28
+ this.modelData = data;
29
+ this._setId(data?._id || null);
30
+ };
33
31
  _handleApiError(response) {
34
32
  return validateApiResponse(response);
35
33
  }
@@ -38,7 +36,7 @@ class BaseModel {
38
36
  }
39
37
  _dataWithoutId() {
40
38
  if (typeof this.modelData === 'object' && this.modelData !== null && '_id' in this.modelData) {
41
- const _a = this.modelData, { _id } = _a, dataWithoutId = __rest(_a, ["_id"]);
39
+ const { _id, ...dataWithoutId } = this.modelData;
42
40
  return dataWithoutId;
43
41
  }
44
42
  return this.modelData;
@@ -80,7 +78,7 @@ class BaseModel {
80
78
  throw new Error(constants.RequiresId);
81
79
  }
82
80
  const updateUrl = buildURL(this.host, this.collection, this.id);
83
- const response = await this.api.patch(updateUrl, this._dataWithoutId())
81
+ const response = await this.apiClient.patch(updateUrl, this._dataWithoutId())
84
82
  .headers({ 'Content-Type': 'application/json' });
85
83
  await this._handleApiError(response);
86
84
  const json = await response.json();
@@ -92,7 +90,7 @@ class BaseModel {
92
90
  throw new Error(constants.RequiresId);
93
91
  }
94
92
  const deleteUrl = buildURL(this.host, this.collection, this.id);
95
- const response = await this.api.delete(deleteUrl);
93
+ const response = await this.apiClient.delete(deleteUrl);
96
94
  await this._handleApiError(response);
97
95
  this._setId(null);
98
96
  this.modelData = {};
@@ -100,7 +98,7 @@ class BaseModel {
100
98
  }
101
99
  async create() {
102
100
  const createUrl = buildURL(this.host, this.collection);
103
- const response = await this.api.post(createUrl, this._dataWithoutId())
101
+ const response = await this.apiClient.post(createUrl, this._dataWithoutId())
104
102
  .headers({ 'Content-Type': 'application/json' });
105
103
  await this._handleApiError(response);
106
104
  const json = await response.json();
@@ -0,0 +1,28 @@
1
+ import { FindManyOptions } from "./createModel.js";
2
+ interface PaginationState {
3
+ skip: number;
4
+ limit: number;
5
+ page: number;
6
+ total: number;
7
+ totalPages: number;
8
+ left: number;
9
+ }
10
+ interface PaginationResultInput<T> {
11
+ data: T[];
12
+ state: PaginationState;
13
+ options: FindManyOptions | {};
14
+ findManyFunction: Function;
15
+ }
16
+ declare class PaginatedResult<T = {}> {
17
+ state: PaginationState;
18
+ data: Array<T>;
19
+ options: FindManyOptions | {};
20
+ private findManyFunction;
21
+ constructor(input: PaginationResultInput<T>);
22
+ next(): Promise<PaginatedResult<T>>;
23
+ previous(): Promise<PaginatedResult<T>>;
24
+ page(pageNumber: number): Promise<this>;
25
+ hasNext(): boolean;
26
+ hasPrevious(): boolean;
27
+ }
28
+ export default PaginatedResult;