@qelos/sdk 2.0.22

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 (80) hide show
  1. package/.nvmrc +1 -0
  2. package/LICENSE +21 -0
  3. package/README.md +47 -0
  4. package/dist/administrator/drafts.d.ts +22 -0
  5. package/dist/administrator/drafts.js +34 -0
  6. package/dist/administrator/drafts.js.map +1 -0
  7. package/dist/administrator/events.d.ts +21 -0
  8. package/dist/administrator/events.js +21 -0
  9. package/dist/administrator/events.js.map +1 -0
  10. package/dist/administrator/index.d.ts +13 -0
  11. package/dist/administrator/index.js +19 -0
  12. package/dist/administrator/index.js.map +1 -0
  13. package/dist/administrator/layouts.d.ts +44 -0
  14. package/dist/administrator/layouts.js +29 -0
  15. package/dist/administrator/layouts.js.map +1 -0
  16. package/dist/administrator/users.d.ts +24 -0
  17. package/dist/administrator/users.js +49 -0
  18. package/dist/administrator/users.js.map +1 -0
  19. package/dist/administrator.d.ts +11 -0
  20. package/dist/administrator.js +16 -0
  21. package/dist/administrator.js.map +1 -0
  22. package/dist/authentication.d.ts +62 -0
  23. package/dist/authentication.js +88 -0
  24. package/dist/authentication.js.map +1 -0
  25. package/dist/base-gp-sdk.d.ts +8 -0
  26. package/dist/base-gp-sdk.js +26 -0
  27. package/dist/base-gp-sdk.js.map +1 -0
  28. package/dist/base-sdk.d.ts +8 -0
  29. package/dist/base-sdk.js +26 -0
  30. package/dist/base-sdk.js.map +1 -0
  31. package/dist/blocks.d.ts +18 -0
  32. package/dist/blocks.js +31 -0
  33. package/dist/blocks.js.map +1 -0
  34. package/dist/categories.d.ts +22 -0
  35. package/dist/categories.js +34 -0
  36. package/dist/categories.js.map +1 -0
  37. package/dist/configurations.d.ts +23 -0
  38. package/dist/configurations.js +19 -0
  39. package/dist/configurations.js.map +1 -0
  40. package/dist/drafts.d.ts +22 -0
  41. package/dist/drafts.js +34 -0
  42. package/dist/drafts.js.map +1 -0
  43. package/dist/index.d.ts +20 -0
  44. package/dist/index.js +42 -0
  45. package/dist/index.js.map +1 -0
  46. package/dist/layouts.d.ts +44 -0
  47. package/dist/layouts.js +29 -0
  48. package/dist/layouts.js.map +1 -0
  49. package/dist/manage-layouts.d.ts +12 -0
  50. package/dist/manage-layouts.js +36 -0
  51. package/dist/manage-layouts.js.map +1 -0
  52. package/dist/menus.d.ts +36 -0
  53. package/dist/menus.js +31 -0
  54. package/dist/menus.js.map +1 -0
  55. package/dist/posts.d.ts +40 -0
  56. package/dist/posts.js +42 -0
  57. package/dist/posts.js.map +1 -0
  58. package/dist/types.d.ts +11 -0
  59. package/dist/types.js +2 -0
  60. package/dist/types.js.map +1 -0
  61. package/dist/users.d.ts +24 -0
  62. package/dist/users.js +49 -0
  63. package/dist/users.js.map +1 -0
  64. package/package.json +18 -0
  65. package/src/administrator/drafts.ts +57 -0
  66. package/src/administrator/events.ts +40 -0
  67. package/src/administrator/index.ts +21 -0
  68. package/src/administrator/layouts.ts +56 -0
  69. package/src/administrator/users.ts +70 -0
  70. package/src/authentication.ts +139 -0
  71. package/src/base-sdk.ts +28 -0
  72. package/src/blocks.ts +49 -0
  73. package/src/categories.ts +62 -0
  74. package/src/configurations.ts +40 -0
  75. package/src/index.ts +45 -0
  76. package/src/manage-layouts.ts +48 -0
  77. package/src/menus.ts +67 -0
  78. package/src/posts.ts +91 -0
  79. package/src/types.ts +10 -0
  80. package/tsconfig.json +14 -0
package/dist/users.js ADDED
@@ -0,0 +1,49 @@
1
+ import BaseSDK from './base-gp-sdk';
2
+ export default class GpUsers extends BaseSDK {
3
+ options;
4
+ relativePath = '/api/users';
5
+ constructor(options) {
6
+ super(options);
7
+ this.options = options;
8
+ }
9
+ getUser(userId) {
10
+ return this.callJsonApi(`${this.relativePath}/${userId}`);
11
+ }
12
+ getList(filters) {
13
+ return this.callJsonApi(this.relativePath +
14
+ (filters ?
15
+ `?${Object.entries(filters).map(([key, value]) => `${key}=${value}`).join('&')}` :
16
+ ''));
17
+ }
18
+ remove(userId) {
19
+ return this.callApi(`${this.relativePath}/${userId}`, { method: 'delete' });
20
+ }
21
+ update(userId, changes) {
22
+ return this.callJsonApi(`${this.relativePath}/${userId}`, {
23
+ method: 'put',
24
+ headers: { 'content-type': 'application/json' },
25
+ body: JSON.stringify(changes)
26
+ });
27
+ }
28
+ create(user) {
29
+ return this.callJsonApi(this.relativePath, {
30
+ method: 'post',
31
+ headers: { 'content-type': 'application/json' },
32
+ body: JSON.stringify(user)
33
+ });
34
+ }
35
+ getEncryptedData(userId) {
36
+ return this.callJsonApi(`${this.relativePath}/${userId}/encrypted`);
37
+ }
38
+ async setEncryptedData(userId, data) {
39
+ const res = await this.callApi(`${this.relativePath}/${userId}/encrypted`, {
40
+ method: 'post',
41
+ headers: { 'content-type': 'application/json' },
42
+ body: JSON.stringify(data)
43
+ });
44
+ if (res.status >= 300) {
45
+ throw new Error('could not set encrypted data');
46
+ }
47
+ }
48
+ }
49
+ //# sourceMappingURL=users.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"users.js","sourceRoot":"","sources":["../src/users.ts"],"names":[],"mappings":"AACA,OAAO,OAAO,MAAM,eAAe,CAAC;AAWpC,MAAM,CAAC,OAAO,OAAO,OAA0B,SAAQ,OAAO;IAGxC;IAFZ,YAAY,GAAG,YAAY,CAAC;IAEpC,YAAoB,OAA6B;QAC/C,KAAK,CAAC,OAAO,CAAC,CAAA;QADI,YAAO,GAAP,OAAO,CAAsB;IAEjD,CAAC;IAED,OAAO,CAAQ,MAAc;QAC3B,OAAO,IAAI,CAAC,WAAW,CAAkB,GAAG,IAAI,CAAC,YAAY,IAAI,MAAM,EAAE,CAAC,CAAA;IAC5E,CAAC;IAED,OAAO,CAAC,OAA4C;QAClD,OAAO,IAAI,CAAC,WAAW,CACrB,IAAI,CAAC,YAAY;YACjB,CAAC,OAAO,CAAC,CAAC;gBACR,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,GAAG,GAAG,IAAI,KAAK,EAAE,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;gBAClF,EAAE,CAAC,CACN,CAAC;IACJ,CAAC;IAED,MAAM,CAAC,MAAc;QACnB,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC,YAAY,IAAI,MAAM,EAAE,EAAE,EAAC,MAAM,EAAE,QAAQ,EAAC,CAAC,CAAC;IAC5E,CAAC;IAED,MAAM,CAAQ,MAAc,EAAE,OAAwC;QACpE,OAAO,IAAI,CAAC,WAAW,CACrB,GAAG,IAAI,CAAC,YAAY,IAAI,MAAM,EAAE,EAChC;YACE,MAAM,EAAE,KAAK;YACb,OAAO,EAAE,EAAC,cAAc,EAAE,kBAAkB,EAAC;YAC7C,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC;SAC9B,CACF,CAAA;IACH,CAAC;IAED,MAAM,CAAQ,IAAoE;QAChF,OAAO,IAAI,CAAC,WAAW,CAAe,IAAI,CAAC,YAAY,EAAE;YACvD,MAAM,EAAE,MAAM;YACd,OAAO,EAAE,EAAC,cAAc,EAAE,kBAAkB,EAAC;YAC7C,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;SAC3B,CAAC,CAAA;IACJ,CAAC;IAED,gBAAgB,CAAQ,MAAc;QACpC,OAAO,IAAI,CAAC,WAAW,CAAkB,GAAG,IAAI,CAAC,YAAY,IAAI,MAAM,YAAY,CAAC,CAAA;IACtF,CAAC;IAED,KAAK,CAAC,gBAAgB,CAAQ,MAAc,EAAE,IAAO;QACnD,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC,YAAY,IAAI,MAAM,YAAY,EAAE;YACzE,MAAM,EAAE,MAAM;YACd,OAAO,EAAE,EAAC,cAAc,EAAE,kBAAkB,EAAC;YAC7C,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;SAC3B,CAAC,CAAA;QACF,IAAI,GAAG,CAAC,MAAM,IAAI,GAAG,EAAE;YACrB,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAA;SAChD;IACH,CAAC;CACF"}
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "@qelos/sdk",
3
+ "version": "2.0.22",
4
+ "description": "SDK for Qelos API",
5
+ "main": "./dist/index.js",
6
+ "scripts": {
7
+ "build": "tsc"
8
+ },
9
+ "author": "David Meir-Levy <davidmeirlevy@gmail.com>",
10
+ "license": "MIT",
11
+ "publishConfig": {
12
+ "access": "public"
13
+ },
14
+ "devDependencies": {
15
+ "typescript": "^4.6.3"
16
+ },
17
+ "gitHead": "c4a69b318689cb5c7d35a05f057e0ed6c9676594"
18
+ }
@@ -0,0 +1,57 @@
1
+ import {QelosSDKOptions} from '../types';
2
+ import BaseSDK from '../base-sdk';
3
+
4
+ export interface IDraftMetadata {
5
+ contextDisplayName?: string;
6
+ contextRouteParams?: any;
7
+ contextType: string;
8
+ contextId?: string;
9
+ contextData: any;
10
+ }
11
+
12
+ export interface IDraft extends IDraftMetadata {
13
+ user: string;
14
+ tenant?: string;
15
+ }
16
+
17
+ export default class GpDrafts extends BaseSDK {
18
+ private relativePath = '/api/drafts';
19
+
20
+ constructor(options: QelosSDKOptions) {
21
+ super(options)
22
+ }
23
+
24
+ #getDraftUrl(contextType, contextId): string {
25
+ const url = new URL(this.relativePath, 'https://stub');
26
+ if (contextType) {
27
+ url.searchParams.append('contextType', contextType)
28
+ }
29
+ if (contextId) {
30
+ url.searchParams.append('contextId', contextId)
31
+ }
32
+ return url.href.replace(url.origin, '');
33
+ }
34
+
35
+ getList() {
36
+ return this.callJsonApi<IDraft[]>(this.relativePath);
37
+ }
38
+
39
+ getDraft(contextType: string, contextId: string) {
40
+ return this.callJsonApi<IDraft>(this.#getDraftUrl(contextType, contextId))
41
+ }
42
+
43
+ setDraft(draft: IDraftMetadata): Promise<IDraft> {
44
+ return this.callJsonApi<IDraft>(
45
+ this.relativePath,
46
+ {
47
+ method: 'put',
48
+ headers: {'content-type': 'application/json'},
49
+ body: JSON.stringify(draft)
50
+ }
51
+ )
52
+ }
53
+
54
+ remove(contextType: string, contextId: string): Promise<any> {
55
+ return this.callApi(this.#getDraftUrl(contextType, contextId), {method: 'delete'});
56
+ }
57
+ }
@@ -0,0 +1,40 @@
1
+ import {QelosSDKOptions} from '../types';
2
+ import BaseSDK from '../base-sdk';
3
+
4
+ export interface IQelosEvent {
5
+ tenant: string;
6
+ user?: string;
7
+ source: string;
8
+ kind: string;
9
+ eventName: string;
10
+ description: string;
11
+ metadata?: any;
12
+ created: Date;
13
+ }
14
+
15
+ export default class QlEvents extends BaseSDK {
16
+ private relativePath = '/api/events';
17
+
18
+ constructor(options: QelosSDKOptions) {
19
+ super(options)
20
+ }
21
+
22
+ getList({page}: { page?: number } = {}) {
23
+ return this.callJsonApi<IQelosEvent[]>(this.relativePath + (page ? ('?page=' + page) : ''));
24
+ }
25
+
26
+ getEvent(eventId: string) {
27
+ return this.callJsonApi<IQelosEvent>(this.relativePath + '/' + eventId)
28
+ }
29
+
30
+ async dispatch(payload: Partial<IQelosEvent>): Promise<void> {
31
+ await this.callApi(
32
+ this.relativePath,
33
+ {
34
+ method: 'post',
35
+ headers: {'content-type': 'application/json'},
36
+ body: JSON.stringify(payload)
37
+ }
38
+ )
39
+ }
40
+ }
@@ -0,0 +1,21 @@
1
+ import QelosSDK from '../index';
2
+ import {QelosSDKOptions} from '../types';
3
+ import GpUsers from './users';
4
+ import GpManageLayouts from '../manage-layouts';
5
+ import GpDrafts from './drafts';
6
+ import QlEvents from './events';
7
+
8
+ export default class QelosAdministratorSDK<T = any> extends QelosSDK {
9
+ users: GpUsers<T>;
10
+ manageLayouts: GpManageLayouts;
11
+ drafts: GpDrafts;
12
+ events: QlEvents;
13
+
14
+ constructor(options: QelosSDKOptions) {
15
+ super(options);
16
+ this.users = new GpUsers<T>(options);
17
+ this.manageLayouts = new GpManageLayouts(options);
18
+ this.drafts = new GpDrafts(options);
19
+ this.events = new QlEvents(options);
20
+ }
21
+ }
@@ -0,0 +1,56 @@
1
+ import { QelosSDKOptions } from "../types";
2
+ import BaseSDK from "../base-sdk";
3
+
4
+ export enum LayoutKind {
5
+ INDEX = "index",
6
+ SEARCH = "search",
7
+ TAG = "tag",
8
+ CATEGORY = "category",
9
+ POST = "post",
10
+ }
11
+
12
+ export enum LayoutConnectedDataKind {
13
+ MENU = "menu",
14
+ BLOCK = "block",
15
+ HTTP = "http",
16
+ POSTS = "posts",
17
+ CATEGORY_POSTS = "categoryPosts",
18
+ CATEGORY = "category",
19
+ POST = "post",
20
+ }
21
+
22
+ export interface ILayoutContent {
23
+ component: string;
24
+ predefined: boolean;
25
+ classes: string;
26
+ children?: ILayoutContent[];
27
+ props: {
28
+ [key: string]: any;
29
+ };
30
+ }
31
+
32
+ export interface ILayout {
33
+ kind: LayoutKind;
34
+ connectedData: Array<{
35
+ kind: LayoutConnectedDataKind;
36
+ data?: any;
37
+ identifier: string;
38
+ reference: string;
39
+ context?: any;
40
+ }>;
41
+ content: ILayoutContent[];
42
+
43
+ [key: string]: any;
44
+ }
45
+
46
+ export default class GpLayouts extends BaseSDK {
47
+ private relativePath = "/api/layouts";
48
+
49
+ constructor(options: QelosSDKOptions) {
50
+ super(options);
51
+ }
52
+
53
+ getLayout(kind: LayoutKind, extra?: Partial<RequestInit>) {
54
+ return this.callJsonApi<ILayout>(`${this.relativePath}/${kind}`, extra);
55
+ }
56
+ }
@@ -0,0 +1,70 @@
1
+ import {QelosSDKOptions} from '../types';
2
+ import BaseSDK from '../base-sdk';
3
+ import {IUser} from '../authentication';
4
+
5
+ export interface IManagedUser<T = any> extends IUser {
6
+ internalMetadata?: T;
7
+ }
8
+
9
+ export interface IManagedUserRequest<T = any> extends IManagedUser<T> {
10
+ password?: string;
11
+ }
12
+
13
+ export default class GpUsers<T = any, E = any> extends BaseSDK {
14
+ private relativePath = '/api/users';
15
+
16
+ constructor(private options: QelosSDKOptions) {
17
+ super(options)
18
+ }
19
+
20
+ getUser<Z = T>(userId: string) {
21
+ return this.callJsonApi<IManagedUser<Z>>(`${this.relativePath}/${userId}`)
22
+ }
23
+
24
+ getList(filters?: { email: string, exact?: boolean }) {
25
+ return this.callJsonApi<IUser[]>(
26
+ this.relativePath +
27
+ (filters ?
28
+ `?${Object.entries(filters).map(([key, value]) => `${key}=${value}`).join('&')}` :
29
+ '')
30
+ );
31
+ }
32
+
33
+ remove(userId: string): Promise<any> {
34
+ return this.callApi(`${this.relativePath}/${userId}`, {method: 'delete'});
35
+ }
36
+
37
+ update<Z = T>(userId: string, changes: Partial<IManagedUserRequest<Z>>): Promise<IManagedUser> {
38
+ return this.callJsonApi<IManagedUser>(
39
+ `${this.relativePath}/${userId}`,
40
+ {
41
+ method: 'put',
42
+ headers: {'content-type': 'application/json'},
43
+ body: JSON.stringify(changes)
44
+ }
45
+ )
46
+ }
47
+
48
+ create<Z = T>(user: Omit<IManagedUserRequest<Z>, '_id' | 'fullName' | 'birthDate'>): Promise<IManagedUser<Z>> {
49
+ return this.callJsonApi<IManagedUser>(this.relativePath, {
50
+ method: 'post',
51
+ headers: {'content-type': 'application/json'},
52
+ body: JSON.stringify(user)
53
+ })
54
+ }
55
+
56
+ getEncryptedData<Z = E>(userId: string) {
57
+ return this.callJsonApi<IManagedUser<Z>>(`${this.relativePath}/${userId}/encrypted`)
58
+ }
59
+
60
+ async setEncryptedData<Z = E>(userId: string, data: Z): Promise<void> {
61
+ const res = await this.callApi(`${this.relativePath}/${userId}/encrypted`, {
62
+ method: 'post',
63
+ headers: {'content-type': 'application/json'},
64
+ body: JSON.stringify(data)
65
+ })
66
+ if (res.status >= 300) {
67
+ throw new Error('could not set encrypted data')
68
+ }
69
+ }
70
+ }
@@ -0,0 +1,139 @@
1
+ import {QelosSDKOptions} from './types';
2
+ import BaseSDK from './base-sdk';
3
+
4
+ export interface IUser {
5
+ _id: string;
6
+ email: string;
7
+ fullName: string;
8
+ firstName: string;
9
+ lastName: string;
10
+ birthDate: string;
11
+ roles: string[];
12
+
13
+ [key: string]: any;
14
+ }
15
+
16
+ export interface ICredentials {
17
+ email: string;
18
+ password: string;
19
+ roles?: string[];
20
+ }
21
+
22
+ export interface BasicPayload {
23
+ user: IUser;
24
+ }
25
+
26
+ export interface ISignupInformation {
27
+ email: string;
28
+ password: string;
29
+ fullName?: string;
30
+ firstName: string;
31
+ lastName: string;
32
+ birthDate: string;
33
+ }
34
+
35
+ export default class GpAuthentication extends BaseSDK {
36
+
37
+ #refreshToken: string;
38
+ #accessToken: string;
39
+
40
+ get accessToken() {
41
+ return this.#accessToken;
42
+ }
43
+
44
+ constructor(options: QelosSDKOptions) {
45
+ super(options);
46
+ if (options.accessToken || options.getAccessToken) {
47
+ this.#accessToken = options.accessToken || options.getAccessToken();
48
+ delete options.accessToken;
49
+ }
50
+ if (options.refreshToken) {
51
+ this.#refreshToken = options.refreshToken;
52
+ delete options.refreshToken;
53
+ }
54
+ }
55
+
56
+ signin(credentials: ICredentials) {
57
+ return this.callJsonApi<{ payload: BasicPayload }>('/api/signin', {
58
+ method: 'post',
59
+ headers: {'content-type': 'application/json'},
60
+ body: JSON.stringify(credentials)
61
+ })
62
+ }
63
+
64
+ oAuthSignin(credentials: ICredentials) {
65
+ return this.callJsonApi<{ payload: BasicPayload & { token: string, refreshToken: string } }>(
66
+ '/api/signin',
67
+ {
68
+ method: 'post',
69
+ headers: {'content-type': 'application/json'},
70
+ body: JSON.stringify({...credentials, authType: 'oauth'})
71
+ })
72
+ .then(data => {
73
+ this.#accessToken = data.payload.token;
74
+ this.#refreshToken = data.payload.refreshToken;
75
+ return data;
76
+ })
77
+ }
78
+
79
+ signup(information: ISignupInformation) {
80
+ return this.callJsonApi<{ payload: BasicPayload }>(
81
+ '/api/signup',
82
+ {
83
+ method: 'post',
84
+ headers: {'content-type': 'application/json'},
85
+ body: JSON.stringify(information)
86
+ })
87
+ }
88
+
89
+ oAuthSignup(information: ISignupInformation) {
90
+ return this.callJsonApi<{ payload: BasicPayload & { token: string, refreshToken: string } }>(
91
+ '/api/signup',
92
+ {
93
+ method: 'post',
94
+ headers: {'content-type': 'application/json'},
95
+ body: JSON.stringify({...information, authType: 'oauth'})
96
+ })
97
+ .then(data => {
98
+ this.#accessToken = data.payload.token;
99
+ this.#refreshToken = data.payload.refreshToken;
100
+ return data;
101
+ })
102
+ }
103
+
104
+ refreshToken(refreshToken?: string) {
105
+ refreshToken = refreshToken || this.#refreshToken;
106
+ if (!refreshToken) {
107
+ throw new Error('existing refresh token not found');
108
+ }
109
+ return this.callJsonApi<{ payload: BasicPayload & { token: string, refreshToken: string } }>(
110
+ '/api/token/refresh',
111
+ {
112
+ method: 'post',
113
+ headers: {
114
+ Authorization: 'Bearer ' + refreshToken
115
+ }
116
+ })
117
+ .then(data => {
118
+ this.#accessToken = data.payload.token;
119
+ this.#refreshToken = data.payload.refreshToken;
120
+ return data;
121
+ })
122
+ }
123
+
124
+ logout() {
125
+ return this.callApi('/api/logout', {method: 'post'})
126
+ }
127
+
128
+ getLoggedInUser() {
129
+ return this.callJsonApi<IUser>('/api/me')
130
+ }
131
+
132
+ updateLoggedInUser(changes: Partial<IUser & { password?: string }>): Promise<IUser> {
133
+ return this.callJsonApi<IUser>('/api/me', {
134
+ method: 'post',
135
+ headers: {'content-type': 'application/json'},
136
+ body: JSON.stringify(changes)
137
+ })
138
+ }
139
+ }
@@ -0,0 +1,28 @@
1
+ import {FetchLike, QelosSDKOptions} from './types';
2
+
3
+ export default class BaseSDK {
4
+ #appUrl: string;
5
+ #fetch: FetchLike;
6
+
7
+ constructor(private qlOptions: QelosSDKOptions) {
8
+ this.#appUrl = qlOptions.appUrl.endsWith('/') ? qlOptions.appUrl.slice(0, -1) : qlOptions.appUrl;
9
+ this.#fetch = qlOptions.fetch;
10
+ }
11
+
12
+ callApi(relativeUrl: string, data?: RequestInit) {
13
+ data = data || {};
14
+ data.headers = data.headers || {};
15
+ Object.assign(data.headers, this.qlOptions.extraHeaders());
16
+ return this.#fetch(this.#appUrl + relativeUrl, data);
17
+ }
18
+
19
+ callJsonApi<T>(relativeUrl: string, data?: RequestInit): Promise<T> {
20
+ return this.callApi(relativeUrl, data).then(async res => {
21
+ const body = await res.json()
22
+ if (res.status >= 300) {
23
+ throw body;
24
+ }
25
+ return body;
26
+ });
27
+ }
28
+ }
package/src/blocks.ts ADDED
@@ -0,0 +1,49 @@
1
+ import {QelosSDKOptions} from './types';
2
+ import BaseSDK from './base-sdk';
3
+
4
+ export interface IBlock {
5
+ name: string;
6
+ description?: string;
7
+ content: string;
8
+ contentType?: 'content' | 'html'
9
+
10
+ [key: string]: any;
11
+ }
12
+
13
+ export default class GpBlocks extends BaseSDK {
14
+ private relativePath = '/api/blocks';
15
+
16
+ constructor(options: QelosSDKOptions) {
17
+ super(options)
18
+ }
19
+
20
+ getBlock(blockId: string) {
21
+ return this.callJsonApi<IBlock>(`${this.relativePath}/${blockId}`)
22
+ }
23
+
24
+ getList() {
25
+ return this.callJsonApi<IBlock[]>(this.relativePath);
26
+ }
27
+
28
+ remove(blockId: string): Promise<any> {
29
+ return this.callApi(`${this.relativePath}/${blockId}`, {method: 'delete'});
30
+ }
31
+
32
+ update(blockId: string, changes: Partial<IBlock>): Promise<IBlock> {
33
+ return this.callJsonApi<IBlock>(
34
+ `${this.relativePath}/${blockId}`,
35
+ {
36
+ method: 'put',
37
+ headers: {'content-type': 'application/json'},
38
+ body: JSON.stringify(changes)}
39
+ )
40
+ }
41
+
42
+ create(block: IBlock): Promise<IBlock> {
43
+ return this.callJsonApi<IBlock>(this.relativePath, {
44
+ method: 'post',
45
+ headers: {'content-type': 'application/json'},
46
+ body: JSON.stringify(block)
47
+ })
48
+ }
49
+ }
@@ -0,0 +1,62 @@
1
+ import { QelosSDKOptions } from "./types";
2
+ import BaseSDK from "./base-sdk";
3
+
4
+ export interface ICategory {
5
+ _id?: string;
6
+ created: Date;
7
+ isPublic: boolean;
8
+ name: string;
9
+ path: string;
10
+ content?: string;
11
+ [key: string]: any;
12
+ }
13
+
14
+ export default class GpCategories extends BaseSDK {
15
+ private relativePath = "/api/categories";
16
+
17
+ constructor(options: QelosSDKOptions) {
18
+ super(options);
19
+ }
20
+
21
+ getByPath(path: string, extra?: Partial<RequestInit>) {
22
+ return this.callJsonApi<ICategory>(
23
+ `${this.relativePath}/${encodeURI(path)}`,
24
+ extra
25
+ );
26
+ }
27
+
28
+ getList(
29
+ options: { target: "front" | "admin" },
30
+ extra?: Partial<RequestInit>
31
+ ) {
32
+ const queryParams = new URLSearchParams(options);
33
+ return this.callJsonApi<ICategory[]>(
34
+ `${this.relativePath}?${queryParams}`,
35
+ extra
36
+ );
37
+ }
38
+
39
+ remove(path: string): Promise<Response> {
40
+ return this.callApi(`${this.relativePath}/${encodeURI(path)}`, {
41
+ method: "delete",
42
+ });
43
+ }
44
+
45
+ update(path: string, changes: Partial<ICategory>): Promise<ICategory> {
46
+ return this.callJsonApi<ICategory>(
47
+ `${this.relativePath}/${encodeURI(path)}`, {
48
+ method: "put",
49
+ headers: {'content-type': 'application/json'},
50
+ body: JSON.stringify(changes)
51
+ }
52
+ );
53
+ }
54
+
55
+ create(category: ICategory): Promise<ICategory> {
56
+ return this.callJsonApi<ICategory>(this.relativePath, {
57
+ method: "post",
58
+ headers: {'content-type': 'application/json'},
59
+ body: JSON.stringify(category),
60
+ });
61
+ }
62
+ }
@@ -0,0 +1,40 @@
1
+ import { QelosSDKOptions } from "./types";
2
+ import BaseSDK from "./base-sdk";
3
+
4
+ export interface IAppConfiguration {
5
+ name: string;
6
+ logoUrl: string;
7
+ description: string;
8
+ keywords: string;
9
+ slogan: string;
10
+ language: string;
11
+ direction: string;
12
+ titleSuffix: string;
13
+ themeStylesUrl: string;
14
+ homeScreen: string;
15
+ colorsPalette: Record<string, string>;
16
+ websiteUrls: string[];
17
+
18
+ [key: string]: any;
19
+ }
20
+
21
+ export default class GpConfigurations extends BaseSDK {
22
+ private relativePath = "/api/configurations/app-configuration";
23
+
24
+ constructor(options: QelosSDKOptions) {
25
+ super(options);
26
+ }
27
+
28
+ getAppConfiguration(extra?: Partial<RequestInit>) {
29
+ return this.callJsonApi<IAppConfiguration>(this.relativePath, extra);
30
+ }
31
+
32
+ update(changes: Partial<IAppConfiguration>, extra?: Partial<RequestInit>): Promise<IAppConfiguration> {
33
+ return this.callJsonApi<IAppConfiguration>(this.relativePath, {
34
+ method: "put",
35
+ headers: {'content-type': 'application/json'},
36
+ body: JSON.stringify(changes),
37
+ ...(extra || {}),
38
+ });
39
+ }
40
+ }
package/src/index.ts ADDED
@@ -0,0 +1,45 @@
1
+ import {QelosSDKOptions} from './types';
2
+ import BaseSDK from './base-sdk';
3
+ import GpCategories from './categories';
4
+ import GpPosts from './posts';
5
+ import GpMenus from './menus';
6
+ import GpConfigurations from './configurations';
7
+ import GpAuthentication from './authentication';
8
+ import GpBlocks from './blocks';
9
+ import GpLayouts from './administrator/layouts';
10
+
11
+ export default class QelosSDK extends BaseSDK {
12
+
13
+ categories: GpCategories;
14
+ posts: GpPosts;
15
+ menus: GpMenus;
16
+ blocks: GpBlocks;
17
+ layouts: GpLayouts;
18
+ configurations: GpConfigurations;
19
+ authentication: GpAuthentication;
20
+
21
+ constructor(private options: QelosSDKOptions) {
22
+ super(options);
23
+ this.categories = new GpCategories(this.options);
24
+ this.posts = new GpPosts(this.options);
25
+ this.menus = new GpMenus(this.options);
26
+ this.blocks = new GpBlocks(this.options);
27
+ this.layouts = new GpLayouts(this.options);
28
+ this.configurations = new GpConfigurations(this.options);
29
+ this.authentication = new GpAuthentication(this.options);
30
+
31
+ if (!options.getAccessToken) {
32
+ options.getAccessToken = () => this.authentication.accessToken;
33
+ }
34
+ if (!options.extraHeaders) {
35
+ options.extraHeaders = () => {
36
+ const token = options.getAccessToken();
37
+ if (token) {
38
+ return {authorization: 'Bearer ' + token}
39
+ }
40
+ return {}
41
+ }
42
+ }
43
+ }
44
+
45
+ }