newt-client-js 1.0.2 → 1.1.0

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/README.md CHANGED
@@ -25,7 +25,7 @@ const { createClient } = require('newt-client-js');
25
25
  const client = createClient({
26
26
  projectUid: 'YOUR_PROJECT_UID',
27
27
  token: 'YOUR_CDN_API_TOKEN',
28
- apiType: 'cdn' // You can specify "cdn" or "content".
28
+ apiType: 'cdn' // You can specify "cdn" or "api".
29
29
  });
30
30
 
31
31
  client
@@ -38,19 +38,6 @@ client
38
38
  .catch((err) => console.log(err));
39
39
  ```
40
40
 
41
- ### Using this library with the Content API
42
-
43
- This library can also be used with the Content API. In order to do so, you need to use the Content API token, available on the same page where you get the CDN API token, such as:
44
-
45
- ```js
46
- const { createClient } = require('newt-client-js');
47
- const client = createClient({
48
- projectUid: 'YOUR_PROJECT_UID',
49
- token: 'YOUR_CONTENT_API_TOKEN',
50
- apiType: 'content'
51
- });
52
- ```
53
-
54
41
  ## Documentation & References
55
42
 
56
43
  Please refer to the [REST API reference](https://developers.newt.so/) for the parameters and operators that can be used for query.
@@ -64,8 +51,8 @@ The `createClient` method supports several options you may set to achieve the ex
64
51
  | Name | Default | Description |
65
52
  | :--- | :--- | :--- |
66
53
  | `projectUid` | | **Required.** Your project uid. |
67
- | `token` | | **Required.** Your CDN API token or Content API token. |
68
- | `apiType` | `cdn` | You can specify "cdn" or "content". |
54
+ | `token` | | **Required.** Your Newt CDN API token or Newt API token. |
55
+ | `apiType` | `cdn` | You can specify `cdn` or `api`. Please specify `cdn` to send a request to the Newt CDN API, or `api` to send a request to the Newt API. |
69
56
 
70
57
  ### Get contents
71
58
 
@@ -122,16 +109,30 @@ client
122
109
  .catch((err) => console.log(err));
123
110
  ```
124
111
 
112
+ ### Get an app
113
+
114
+ ```js
115
+ client
116
+ .getApp({
117
+ appUid: 'YOUR_APP_UID'
118
+ })
119
+ .then((app) => console.log(app))
120
+ .catch((err) => console.log(err));
121
+ ```
122
+
125
123
  ### Usage with TypeScript
126
124
 
127
- The type of the content you want to get can be passed as a parameter.
125
+ #### Type definition
126
+
127
+ By using the type Content, you can easily define the type.
128
128
 
129
129
  ```ts
130
130
  // Suppose you have defined a model named Post in the admin page.
131
131
 
132
132
  // Type definition
133
133
  /**
134
- * // Content type
134
+ * Content type
135
+ *
135
136
  * {
136
137
  * _id: string;
137
138
  * _sys: {
@@ -153,10 +154,16 @@ interface Post extends Content {
153
154
  title: string
154
155
  body: string
155
156
  }
157
+ ```
158
+
159
+ #### Request and Response
156
160
 
157
- // Request
161
+ The type of the content you want to get can be passed as a parameter.
162
+
163
+ ```ts
158
164
  /**
159
- * // getContents response type
165
+ * getContents response type
166
+ *
160
167
  * {
161
168
  * skip: number;
162
169
  * limit: number;
@@ -172,7 +179,8 @@ client.getContents<Post>({
172
179
  .catch((err) => console.log(err));
173
180
 
174
181
  /**
175
- * // getContent response type
182
+ * getContent response type
183
+ *
176
184
  * {
177
185
  * _id: string;
178
186
  * _sys: {
@@ -201,6 +209,37 @@ client
201
209
  .catch((err) => console.log(err));
202
210
  ```
203
211
 
212
+ #### Query Fields
213
+
214
+ All fields are optional.
215
+
216
+ | Field | Type | Description |
217
+ | :--- | :--- | :--- |
218
+ | `YOUR_FIELD` | - | You can define a query for a field that you define |
219
+ | `select` | string[] | |
220
+ | `order` | string[] | |
221
+ | `limit` | number | |
222
+ | `skip` | number| |
223
+ | `depth` | number | |
224
+ | `or` | Array | Connects each element in the array as an "or" condition. |
225
+ | `and` | Array | Connects each element in the array as an "and" condition. |
226
+
227
+ #### Query Operators
228
+
229
+ | Operator | Type |
230
+ | :--- | :--- |
231
+ | `ne` | string / number / boolean |
232
+ | `match` | string |
233
+ | `in` | string[] / number[] |
234
+ | `nin` | string[] / number[] |
235
+ | `all` | string[] / number[] |
236
+ | `exists` | boolean |
237
+ | `lt` | string / number |
238
+ | `lte` | string / number |
239
+ | `gt` | string / number |
240
+ | `gte` | string / number |
241
+ | `fmt` | 'text' |
242
+
204
243
  ## License
205
244
 
206
245
  This repository is published under the [MIT License](https://github.com/Newt-Inc/newt-client-js/blob/main/LICENSE).
@@ -1,5 +1,6 @@
1
- import { CreateClientParams, GetContentsParams, GetContentParams, Contents } from './types';
1
+ import { CreateClientParams, GetContentsParams, GetContentParams, Contents, GetAppParams, AppMeta } from './types';
2
2
  export declare const createClient: ({ projectUid, token, apiType, }: CreateClientParams) => {
3
3
  getContents: <T>({ appUid, modelUid, query, }: GetContentsParams) => Promise<Contents<T>>;
4
4
  getContent: <T_1>({ appUid, modelUid, contentId, query, }: GetContentParams) => Promise<T_1>;
5
+ getApp: ({ appUid }: GetAppParams) => Promise<AppMeta | null>;
5
6
  };
@@ -41,37 +41,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
41
41
  Object.defineProperty(exports, "__esModule", { value: true });
42
42
  exports.createClient = void 0;
43
43
  var axios_1 = __importDefault(require("axios"));
44
- var qs_1 = __importDefault(require("qs"));
45
- var parseOrQuery = function (orQuery) {
46
- var orConditions = [];
47
- orQuery.forEach(function (query) {
48
- if (query.or) {
49
- orConditions.push(parseOrQuery(query.or));
50
- }
51
- else {
52
- orConditions.push(qs_1.default.stringify(query, { encode: false, arrayFormat: 'comma' }));
53
- }
54
- });
55
- var q = '[or]=(' + orConditions.join(';') + ')';
56
- return q;
57
- };
58
- var parseQuery = function (query) {
59
- if (!query.or)
60
- return qs_1.default.stringify(query, { encode: false, arrayFormat: 'comma' });
61
- var orQuery = parseOrQuery(query.or);
62
- delete query.or;
63
- var q = qs_1.default.stringify(query, { encode: false, arrayFormat: 'comma' });
64
- q = q ? q + '&' + orQuery : orQuery;
65
- return q;
66
- };
44
+ var parseQuery_1 = require("./utils/parseQuery");
67
45
  var createClient = function (_a) {
68
46
  var projectUid = _a.projectUid, token = _a.token, _b = _a.apiType, apiType = _b === void 0 ? 'cdn' : _b;
69
47
  if (!projectUid)
70
48
  throw new Error('projectUid parameter is required.');
71
49
  if (!token)
72
50
  throw new Error('token parameter is required.');
73
- if (!['cdn', 'content'].includes(apiType))
74
- throw new Error("apiType parameter should be set to \"cdn\" or \"content\". apiType: " + apiType);
51
+ if (!['cdn', 'api'].includes(apiType))
52
+ throw new Error("apiType parameter should be set to \"cdn\" or \"api\". apiType: " + apiType);
75
53
  var axiosInstance = axios_1.default.create({
76
54
  baseURL: "https://" + projectUid + "." + apiType + ".newt.so/v1",
77
55
  headers: { Authorization: "Bearer " + token },
@@ -89,7 +67,7 @@ var createClient = function (_a) {
89
67
  throw new Error('model parameter is required.');
90
68
  url = "/" + appUid + "/" + modelUid;
91
69
  if (query && Object.keys(query).length) {
92
- url = url + '?' + parseQuery(query);
70
+ url = url + '?' + (0, parseQuery_1.parseQuery)(query);
93
71
  }
94
72
  return [4 /*yield*/, axiosInstance.get(url)];
95
73
  case 1:
@@ -114,7 +92,7 @@ var createClient = function (_a) {
114
92
  throw new Error('contentId parameter is required.');
115
93
  url = "/" + appUid + "/" + modelUid + "/" + contentId;
116
94
  if (query && Object.keys(query).length) {
117
- url = url + '?' + parseQuery(query);
95
+ url = url + '?' + (0, parseQuery_1.parseQuery)(query);
118
96
  }
119
97
  return [4 /*yield*/, axiosInstance.get(url)];
120
98
  case 1:
@@ -124,9 +102,28 @@ var createClient = function (_a) {
124
102
  });
125
103
  });
126
104
  };
105
+ var getApp = function (_a) {
106
+ var appUid = _a.appUid;
107
+ return __awaiter(void 0, void 0, void 0, function () {
108
+ var url, data;
109
+ return __generator(this, function (_b) {
110
+ switch (_b.label) {
111
+ case 0:
112
+ if (!appUid)
113
+ throw new Error('appUid parameter is required.');
114
+ url = "/project/apps/" + appUid;
115
+ return [4 /*yield*/, axiosInstance.get(url)];
116
+ case 1:
117
+ data = (_b.sent()).data;
118
+ return [2 /*return*/, data];
119
+ }
120
+ });
121
+ });
122
+ };
127
123
  return {
128
124
  getContents: getContents,
129
125
  getContent: getContent,
126
+ getApp: getApp,
130
127
  };
131
128
  };
132
129
  exports.createClient = createClient;
package/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export { createClient } from './createClient';
2
- export { CreateClientParams, GetContentsParams, GetContentParams, GetContentsQuery, GetContentQuery, Contents, Content, } from './types';
2
+ export { CreateClientParams, GetContentsParams, GetContentParams, GetContentsQuery, GetContentQuery, Contents, Content, AppMeta, AppCover, AppIcon, GetAppParams, } from './types';
package/dist/types.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  export interface CreateClientParams {
2
2
  projectUid: string;
3
3
  token: string;
4
- apiType?: 'cdn' | 'content';
4
+ apiType?: 'cdn' | 'api';
5
5
  }
6
6
  export interface GetContentsParams {
7
7
  appUid: string;
@@ -28,11 +28,10 @@ declare type OperatorValue = {
28
28
  fmt?: 'text';
29
29
  };
30
30
  declare type QueryValue = string | number | boolean | OperatorValue;
31
- declare type AtomicQuery = {
32
- [key: string]: QueryValue;
33
- };
34
- export declare type OrQuery = {
35
- or: Array<AtomicQuery | OrQuery>;
31
+ export declare type FilterQuery = {
32
+ or?: Array<FilterQuery>;
33
+ and?: Array<FilterQuery>;
34
+ [key: string]: QueryValue | Array<FilterQuery> | undefined;
36
35
  };
37
36
  export declare type Query = {
38
37
  select?: string[];
@@ -40,8 +39,9 @@ export declare type Query = {
40
39
  limit?: number;
41
40
  skip?: number;
42
41
  depth?: number;
43
- or?: Array<AtomicQuery | OrQuery>;
44
- [key: string]: QueryValue | string[] | Array<AtomicQuery | OrQuery> | undefined;
42
+ or?: Array<FilterQuery>;
43
+ and?: Array<FilterQuery>;
44
+ [key: string]: QueryValue | string[] | Array<FilterQuery> | undefined;
45
45
  };
46
46
  export declare type GetContentsQuery = Query;
47
47
  declare type ExceptFormat = {
@@ -75,4 +75,21 @@ export interface Content {
75
75
  };
76
76
  };
77
77
  }
78
+ export declare type AppIcon = {
79
+ type: string;
80
+ value: string;
81
+ };
82
+ export declare type AppCover = {
83
+ type: string;
84
+ value: string;
85
+ };
86
+ export interface AppMeta {
87
+ name: string;
88
+ uid: string;
89
+ icon?: AppIcon;
90
+ cover?: AppCover;
91
+ }
92
+ export interface GetAppParams {
93
+ appUid: string;
94
+ }
78
95
  export {};
@@ -0,0 +1,2 @@
1
+ import { Query } from '../types';
2
+ export declare const parseQuery: (query: Query) => string;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.parseQuery = void 0;
7
+ var qs_1 = __importDefault(require("qs"));
8
+ var parseAndQuery = function (andQuery) {
9
+ if (!andQuery)
10
+ throw new Error('invalid query');
11
+ var andConditions = [];
12
+ andQuery.forEach(function (query) {
13
+ andConditions.push((0, exports.parseQuery)(query));
14
+ });
15
+ var q = andConditions.join('&');
16
+ return q;
17
+ };
18
+ var parseOrQuery = function (orQuery) {
19
+ if (!orQuery)
20
+ throw new Error('invalid query');
21
+ var orConditions = [];
22
+ orQuery.forEach(function (query) {
23
+ orConditions.push((0, exports.parseQuery)(query));
24
+ });
25
+ var q = '[or]=(' + orConditions.join(';') + ')';
26
+ return q;
27
+ };
28
+ var parseQuery = function (query) {
29
+ var andQuery = '';
30
+ if (query.and) {
31
+ andQuery = parseAndQuery(query.and);
32
+ delete query.and;
33
+ }
34
+ var orQuery = '';
35
+ if (query.or) {
36
+ orQuery = parseOrQuery(query.or);
37
+ delete query.or;
38
+ }
39
+ var q = qs_1.default.stringify(query, { encode: false, arrayFormat: 'comma' });
40
+ q = [q, orQuery, andQuery].filter(function (queryString) { return queryString; }).join('&');
41
+ return q;
42
+ };
43
+ exports.parseQuery = parseQuery;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "newt-client-js",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "description": "JavaScript SDK for Newt's API",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -19,20 +19,40 @@
19
19
  "lint:prettier": "prettier --check ./src",
20
20
  "fix:eslint": "prettier --fix ./src",
21
21
  "fix:prettier": "prettier --write ./src",
22
- "test": "echo \"Error: no test specified\" && exit 1"
22
+ "test": "jest"
23
+ },
24
+ "jest": {
25
+ "moduleFileExtensions": [
26
+ "ts",
27
+ "js"
28
+ ],
29
+ "transform": {
30
+ "^.+\\.ts$": "ts-jest"
31
+ },
32
+ "globals": {
33
+ "ts-jest": {
34
+ "tsconfig": "tests/tsconfig.json"
35
+ }
36
+ },
37
+ "testMatch": [
38
+ "**/tests/**/*.test.ts"
39
+ ]
23
40
  },
24
41
  "dependencies": {
25
42
  "axios": "^0.21.1",
26
43
  "qs": "^6.10.1"
27
44
  },
28
45
  "devDependencies": {
46
+ "@types/jest": "^27.0.3",
29
47
  "@types/node": "^16.7.8",
30
48
  "@types/qs": "^6.9.7",
31
49
  "@typescript-eslint/eslint-plugin": "^4.30.0",
32
50
  "@typescript-eslint/parser": "^4.30.0",
33
51
  "eslint": "^7.32.0",
34
52
  "eslint-config-prettier": "^8.3.0",
53
+ "jest": "^27.4.3",
35
54
  "prettier": "^2.3.2",
55
+ "ts-jest": "^27.1.0",
36
56
  "typescript": "^4.4.2"
37
57
  }
38
58
  }