kodzero-front-sdk-alfa 0.0.9 → 0.0.15

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/Kodzero.d.ts CHANGED
@@ -4,15 +4,17 @@ import { ModelOptions } from "./model/BaseModel.js";
4
4
  import TokensManagerClass from "./auth/tokens.js";
5
5
  interface Options {
6
6
  host: string;
7
+ collection: string;
7
8
  }
8
9
  declare class Kodzero {
9
10
  host: string;
11
+ collection: string;
10
12
  auth: KodzeroAuth;
11
13
  tokensManager: TokensManagerClass;
12
14
  api: typeof FluidFetch;
13
15
  constructor(options: Options);
14
16
  createModel: <T extends {
15
- _id?: string;
17
+ _id: string | null;
16
18
  }, M = {}>(options: Omit<ModelOptions, "host">) => {
17
19
  new (data: T): import("./model/BaseModel.js").default<T> & M;
18
20
  get(id: string): Promise<import("./model/BaseModel.js").default<T> & M>;
package/dist/Kodzero.js CHANGED
@@ -4,14 +4,19 @@ import createModel from "./model/createModel.js";
4
4
  import TokensManagerClass from "./auth/tokens.js";
5
5
  class Kodzero {
6
6
  host;
7
+ collection;
7
8
  auth;
8
9
  tokensManager;
9
10
  api;
10
11
  constructor(options) {
11
12
  this.tokensManager = new TokensManagerClass('', '');
13
+ this.collection = options.collection;
12
14
  this.host = options.host;
13
15
  this.api = new FluidFetch();
14
- this.auth = new KodzeroAuth({ host: options.host }, this.api, this.tokensManager);
16
+ this.auth = new KodzeroAuth({
17
+ host: options.host,
18
+ collection: options.collection
19
+ }, this.api, this.tokensManager);
15
20
  this.api.middlewares.request.use((req) => {
16
21
  const accessToken = this.tokensManager.access;
17
22
  if (accessToken)
@@ -39,7 +39,7 @@ type RefreshResponse = SuccessResponse<{
39
39
  }>;
40
40
  declare class KodzeroAuthEmail extends KodzeroAuthBase {
41
41
  tokensManager: TokensManagerClass;
42
- collection: "auth/password";
42
+ collection: string;
43
43
  constructor(options: AuthOptions, api: typeof FluidFetch, tokensManager: TokensManagerClass);
44
44
  _setTokens: (access: string, refresh?: string) => void;
45
45
  login: (input: KodzeroAuthEmailLogin) => Promise<LoginResponse["result"]>;
@@ -6,7 +6,7 @@ class KodzeroAuthEmail extends KodzeroAuthBase {
6
6
  constructor(options, api, tokensManager) {
7
7
  super(options, api, tokensManager);
8
8
  this.tokensManager = tokensManager;
9
- this.collection = "auth/password";
9
+ this.collection = `${options.collection}/password`;
10
10
  }
11
11
  _setTokens = (access, refresh) => {
12
12
  this.tokensManager.setAccess(access);
@@ -4,6 +4,7 @@ import KodzeroAuthEmail from "./email.js";
4
4
  import TokensManagerClass from "./tokens.js";
5
5
  export interface AuthOptions {
6
6
  host: string;
7
+ collection: string;
7
8
  }
8
9
  declare class KodzeroAuth extends KodzeroAuthBase {
9
10
  email: KodzeroAuthEmail;
@@ -6,7 +6,7 @@ export interface ModelOptions {
6
6
  schema?: Record<string, any>;
7
7
  }
8
8
  declare class BaseModel<T extends {
9
- _id?: string;
9
+ _id: string | null;
10
10
  }> {
11
11
  host: string;
12
12
  collection: string;
@@ -9,7 +9,7 @@ export interface FindManyOptions {
9
9
  fields?: string[];
10
10
  }
11
11
  declare const createModel: <T extends {
12
- _id?: string;
12
+ _id: string | null;
13
13
  }, M = {}>(options: ModelOptions, apiClient: typeof FluidFetch) => {
14
14
  new (data: T): BaseModel<T> & M;
15
15
  get(id: string): Promise<BaseModel<T> & M>;
@@ -18,7 +18,10 @@ const createModel = (options, apiClient) => {
18
18
  const response = await Model.apiClient.get(getUrl);
19
19
  await Model._handleApiError(response);
20
20
  const json = await response.json();
21
- return new Model(json.result);
21
+ const model = new Model(json.result);
22
+ model.modelData._id = json.result._id;
23
+ model.id = json.result._id;
24
+ return model;
22
25
  }
23
26
  static registerMethod(name, fn) {
24
27
  Model.prototype[name] = fn;
@@ -148,8 +151,9 @@ const createModel = (options, apiClient) => {
148
151
  }
149
152
  constructor(data) {
150
153
  super(options, apiClient);
151
- this.modelData = { ...data, _id: null };
152
- this.id = null;
154
+ this.modelData = { ...data };
155
+ this.modelData._id = data._id || null;
156
+ this.id = data._id || null;
153
157
  }
154
158
  };
155
159
  return Model;
@@ -9,7 +9,7 @@ class BaseModelSchema {
9
9
  type: String
10
10
  },
11
11
  schema: {
12
- type: Schema,
12
+ type: Object,
13
13
  required: false
14
14
  }
15
15
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kodzero-front-sdk-alfa",
3
- "version": "0.0.9",
3
+ "version": "0.0.15",
4
4
  "main": "dist/index.js",
5
5
  "scripts": {
6
6
  "tsc": "tsc -b src/",
package/src/Kodzero.ts CHANGED
@@ -6,19 +6,25 @@ import TokensManagerClass from "./auth/tokens.js"
6
6
 
7
7
  interface Options {
8
8
  host: string
9
+ collection: string
9
10
  }
10
11
 
11
12
  class Kodzero {
12
13
  host: string
14
+ collection: string
13
15
  auth: KodzeroAuth
14
16
  tokensManager: TokensManagerClass
15
17
  api: typeof FluidFetch
16
18
 
17
19
  constructor (options: Options) {
18
20
  this.tokensManager = new TokensManagerClass('', '')
21
+ this.collection = options.collection
19
22
  this.host = options.host
20
23
  this.api = new FluidFetch()
21
- this.auth = new KodzeroAuth({host: options.host}, this.api, this.tokensManager)
24
+ this.auth = new KodzeroAuth({
25
+ host: options.host,
26
+ collection: options.collection
27
+ }, this.api, this.tokensManager)
22
28
 
23
29
  this.api.middlewares.request.use((req: any) => {
24
30
  const accessToken = this.tokensManager.access
@@ -27,7 +33,7 @@ class Kodzero {
27
33
  })
28
34
  }
29
35
 
30
- createModel = <T extends { _id?: string }, M = {}>(options: Omit<ModelOptions, 'host'>) => {
36
+ createModel = <T extends { _id: string | null }, M = {}>(options: Omit<ModelOptions, 'host'>) => {
31
37
  return createModel<T, M>({...options, host: this.host}, this.api)
32
38
  }
33
39
  }
@@ -2,7 +2,8 @@ import Kodzero from '../Kodzero.js';
2
2
 
3
3
  it('should create an instance if host provided', async () => {
4
4
  const kodzero = new Kodzero({
5
- host: 'http://localhost:6969'
5
+ host: 'http://localhost:6969',
6
+ collection: "_auth_"
6
7
  });
7
8
 
8
9
  expect(kodzero).toBeInstanceOf(Kodzero);
package/src/auth/email.ts CHANGED
@@ -47,13 +47,13 @@ type LogoutResponse = SuccessResponse<boolean>
47
47
 
48
48
  class KodzeroAuthEmail extends KodzeroAuthBase {
49
49
  tokensManager: TokensManagerClass
50
- collection: "auth/password"
50
+ collection: string
51
51
 
52
52
  constructor(options: AuthOptions, api: typeof FluidFetch, tokensManager: TokensManagerClass) {
53
53
  super(options, api, tokensManager)
54
54
 
55
55
  this.tokensManager = tokensManager
56
- this.collection = "auth/password"
56
+ this.collection = `${options.collection}/password`
57
57
  }
58
58
 
59
59
  /**
package/src/auth/index.ts CHANGED
@@ -5,6 +5,7 @@ import TokensManagerClass from "./tokens.js";
5
5
 
6
6
  export interface AuthOptions {
7
7
  host: string
8
+ collection: string
8
9
  }
9
10
 
10
11
  class KodzeroAuth extends KodzeroAuthBase {
@@ -15,7 +15,7 @@ export interface ModelOptions {
15
15
  /**
16
16
  * Base model constructor that expose main methods to work with user-created model
17
17
  */
18
- class BaseModel<T extends { _id?: string }> {
18
+ class BaseModel<T extends { _id: string | null }> {
19
19
  host: string
20
20
  collection: string
21
21
  modelData: T = {} as T
@@ -18,7 +18,7 @@ export interface FindManyOptions {
18
18
  * Creates custom model with the specified schema, API options, etc.
19
19
  */
20
20
  const createModel = <
21
- T extends { _id?: string },
21
+ T extends { _id: string | null},
22
22
  M = {}
23
23
  >(options: ModelOptions, apiClient: typeof FluidFetch) => {
24
24
  const Model = class extends BaseModel<T> {
@@ -40,8 +40,11 @@ const createModel = <
40
40
  await Model._handleApiError(response);
41
41
 
42
42
  const json = await response.json();
43
+ const model = new Model(json.result);
44
+ model.modelData._id = json.result._id;
45
+ model.id = json.result._id;
43
46
 
44
- return new Model(json.result);
47
+ return model;
45
48
  }
46
49
 
47
50
  /**
@@ -106,7 +109,7 @@ const createModel = <
106
109
 
107
110
  static async create(data: T): Promise<T> {
108
111
  const createUrl = buildURL(Model.host, Model.collection)
109
- const {_id, ...dataWithoutId} = data as T & {_id?: string};
112
+ const {_id, ...dataWithoutId} = data as T & {_id: string | null};
110
113
 
111
114
  const response = await Model.apiClient.post(createUrl)
112
115
  .body(dataWithoutId)
@@ -230,8 +233,9 @@ const createModel = <
230
233
  constructor(data: T) {
231
234
  super(options, apiClient)
232
235
 
233
- this.modelData = {...data, _id: null}
234
- this.id = null;
236
+ this.modelData = {...data}
237
+ this.modelData._id = data._id || null;
238
+ this.id = data._id || null;
235
239
  }
236
240
  }
237
241
 
@@ -11,7 +11,7 @@ class BaseModelSchema {
11
11
  type: String
12
12
  },
13
13
  schema: {
14
- type: Schema,
14
+ type: Object,
15
15
  required: false
16
16
  }
17
17
  })