@jd-data-limited/easy-fm 0.0.10 → 0.0.12

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 (3) hide show
  1. package/index.js +19 -10
  2. package/index.ts +18 -10
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -147,11 +147,12 @@ class layout {
147
147
  createRecord() {
148
148
  return new Promise((resolve, reject) => {
149
149
  // Get the layout's metadata
150
- this.conn.apiRequest(`${this.endpoint}`).then(res => {
150
+ this.getLayoutMeta().then(layout => {
151
151
  let fields = {};
152
- for (let _field of res.response.fieldMetaData) {
152
+ for (let _field of this.metadata.fieldMetaData) {
153
153
  fields[_field.name] = "";
154
154
  }
155
+ console.log(fields);
155
156
  resolve(new record(this, -1, 0, fields));
156
157
  });
157
158
  });
@@ -233,6 +234,9 @@ class record extends EventEmitter {
233
234
  });
234
235
  }
235
236
  get() {
237
+ if (this.recordId === -1) {
238
+ throw "Cannot get this record until a commit() is done.";
239
+ }
236
240
  return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
237
241
  if (!this.layout.metadata)
238
242
  yield this.layout.getLayoutMeta();
@@ -259,24 +263,26 @@ class record extends EventEmitter {
259
263
  });
260
264
  }));
261
265
  }
262
- commit(extraBody = {}) {
266
+ commit(extraBody = {
267
+ fieldData: undefined
268
+ }) {
263
269
  return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
264
270
  let data = this.toObject();
265
271
  delete data.recordId;
266
272
  delete data.modId;
267
273
  if (this.recordId === -1) {
268
274
  // This is a new record
275
+ extraBody.fieldData = data.fieldData;
269
276
  this.layout.conn.apiRequest(`${this.layout.endpoint}/records`, {
270
277
  port: 443,
271
278
  method: "POST",
272
- body: JSON.stringify({
273
- fieldData: data.fieldData
274
- })
279
+ body: JSON.stringify(extraBody)
275
280
  })
276
281
  .then(res => {
277
282
  if (res.messages[0].code === "0") {
278
- this.recordId = res.response.recordId;
279
- this.modId = res.response.modId;
283
+ this.recordId = parseInt(res.response.recordId);
284
+ console.log(this.recordId);
285
+ this.modId = parseInt(res.response.modId);
280
286
  resolve(this);
281
287
  }
282
288
  else {
@@ -412,7 +418,7 @@ class portalItem extends record {
412
418
  attachPortal(portal) {
413
419
  this.portal = portal;
414
420
  }
415
- commit(extraBody = {}) {
421
+ commit(extraBody = { fieldData: undefined }) {
416
422
  return this.portal.record.commit(extraBody);
417
423
  }
418
424
  toObject(fieldFilter) {
@@ -489,9 +495,12 @@ class find {
489
495
  }
490
496
  export class FMError extends Error {
491
497
  constructor(code, httpStatus, res) {
492
- super(errs.find(err => err.e === code) || "Unknown error");
498
+ if (typeof code === "string")
499
+ code = parseInt(code);
500
+ super(errs.find(err => err.e === code).d || "Unknown error");
493
501
  this.httpStatus = httpStatus;
494
502
  this.res = res;
503
+ this.messages = res.messages;
495
504
  this.code = typeof code === "string" ? parseInt(code) : code;
496
505
  Error.captureStackTrace(this, FMError);
497
506
  }
package/index.ts CHANGED
@@ -181,11 +181,12 @@ class layout {
181
181
  createRecord(): Promise<record | Error | void> {
182
182
  return new Promise((resolve, reject) => {
183
183
  // Get the layout's metadata
184
- this.conn.apiRequest(`${this.endpoint}`).then(res => {
184
+ this.getLayoutMeta().then(layout => {
185
185
  let fields = {}
186
- for (let _field of res.response.fieldMetaData) {
186
+ for (let _field of this.metadata.fieldMetaData) {
187
187
  fields[_field.name] = ""
188
188
  }
189
+ console.log(fields)
189
190
  resolve(new record(this, -1, 0, fields))
190
191
  })
191
192
  })
@@ -281,6 +282,7 @@ class record extends EventEmitter {
281
282
  }
282
283
 
283
284
  get() {
285
+ if (this.recordId === -1) {throw "Cannot get this record until a commit() is done."}
284
286
  return new Promise(async (resolve, reject) => {
285
287
  if (!this.layout.metadata) await this.layout.getLayoutMeta()
286
288
 
@@ -306,7 +308,9 @@ class record extends EventEmitter {
306
308
  })
307
309
  }
308
310
 
309
- commit(extraBody = {}): Promise<record | FMError> {
311
+ commit(extraBody = {
312
+ fieldData: undefined
313
+ }): Promise<record | FMError> {
310
314
  return new Promise(async (resolve, reject) => {
311
315
  let data = this.toObject()
312
316
  delete data.recordId
@@ -314,17 +318,18 @@ class record extends EventEmitter {
314
318
 
315
319
  if (this.recordId === -1) {
316
320
  // This is a new record
321
+ extraBody.fieldData = data.fieldData
322
+
317
323
  this.layout.conn.apiRequest(`${this.layout.endpoint}/records`, {
318
324
  port: 443,
319
325
  method: "POST",
320
- body: JSON.stringify({
321
- fieldData: data.fieldData
322
- })
326
+ body: JSON.stringify(extraBody)
323
327
  })
324
328
  .then(res => {
325
329
  if (res.messages[0].code === "0") {
326
- this.recordId = res.response.recordId
327
- this.modId = res.response.modId
330
+ this.recordId = parseInt(res.response.recordId)
331
+ console.log(this.recordId)
332
+ this.modId = parseInt(res.response.modId)
328
333
  resolve(this)
329
334
  } else {
330
335
  reject(new FMError(res.messages[0].code, res.status, res))
@@ -480,7 +485,7 @@ class portalItem extends record {
480
485
  this.portal = portal
481
486
  }
482
487
 
483
- commit(extraBody = {}) {
488
+ commit(extraBody = {fieldData: undefined}) {
484
489
  return this.portal.record.commit(extraBody)
485
490
  }
486
491
 
@@ -568,11 +573,14 @@ export class FMError extends Error {
568
573
  readonly res: any
569
574
  readonly code: number
570
575
  readonly message: string
576
+ readonly messages: string
571
577
 
572
578
  constructor(code, httpStatus, res) {
573
- super(errs.find(err => err.e === code) || "Unknown error");
579
+ if (typeof code === "string") code = parseInt(code)
580
+ super(errs.find(err => err.e === code).d || "Unknown error");
574
581
  this.httpStatus = httpStatus
575
582
  this.res = res
583
+ this.messages = res.messages
576
584
  this.code = typeof code === "string" ? parseInt(code) : code
577
585
  Error.captureStackTrace(this, FMError)
578
586
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jd-data-limited/easy-fm",
3
- "version": "0.0.10",
3
+ "version": "0.0.12",
4
4
  "dependencies": {
5
5
  "@types/btoa": "^1.2.3",
6
6
  "@types/events": "^3.0.0",