@cntrl-site/sdk 0.3.0 → 0.4.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.
Binary file
@@ -51,6 +51,12 @@ class Client {
51
51
  const typePresets = core_1.TypePresetsSchema.parse(data);
52
52
  return typePresets;
53
53
  }
54
+ async getKeyframes(articleId) {
55
+ const response = await this.fetchKeyframes(articleId);
56
+ const data = await response.json();
57
+ const keyframes = core_1.KeyframesSchema.parse(data);
58
+ return keyframes;
59
+ }
54
60
  static getPageMeta(projectMeta, pageMeta) {
55
61
  return pageMeta.enabled ? {
56
62
  title: pageMeta.title ? pageMeta.title : projectMeta.title,
@@ -76,6 +82,14 @@ class Client {
76
82
  }
77
83
  return response;
78
84
  }
85
+ async fetchKeyframes(articleId) {
86
+ const url = new url_1.URL(`/keyframes/${articleId}`, this.APIUrl);
87
+ const response = await this.fetchImpl(url.href);
88
+ if (!response.ok) {
89
+ throw new Error(`Failed to fetch keyframes for the article with id #${articleId}: ${response.statusText}`);
90
+ }
91
+ return response;
92
+ }
79
93
  async fetchTypePresets() {
80
94
  const url = new url_1.URL(`/projects/${this.projectId}/type-presets`, this.APIUrl);
81
95
  const response = await this.fetchImpl(url.href);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cntrl-site/sdk",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Generic SDK for use in public websites.",
5
5
  "main": "lib/index.js",
6
6
  "types": "src/index.ts",
@@ -24,7 +24,7 @@
24
24
  "lib": "lib"
25
25
  },
26
26
  "dependencies": {
27
- "@cntrl-site/core": "^1.14.0",
27
+ "@cntrl-site/core": "^1.14.1",
28
28
  "@types/isomorphic-fetch": "^0.0.36",
29
29
  "isomorphic-fetch": "^3.0.0",
30
30
  "url": "^0.11.0"
@@ -3,6 +3,7 @@ import { projectMock } from './__mock__/projectMock';
3
3
  import { articleMock } from './__mock__/articleMock';
4
4
  import { typePresetsMock } from './__mock__/typePresetsMock';
5
5
  import { TMeta, TPageMeta } from '@cntrl-site/core';
6
+ import { keyframesMock } from './__mock__/keyframesMock';
6
7
 
7
8
  describe('Client', () => {
8
9
  it('returns project', async () => {
@@ -143,6 +144,43 @@ describe('Client', () => {
143
144
  );
144
145
  });
145
146
 
147
+ it('returns keyframes by article id', async () => {
148
+ let fetchCalledTimes = 0;
149
+ const projectId = 'projectId';
150
+ const articleId = 'articleId';
151
+ const apiUrl = 'https://api.cntrl.site/';
152
+ const fetch = (url: string) => {
153
+ fetchCalledTimes += 1;
154
+ expect(url).toBe(`${apiUrl}keyframes/${articleId}`);
155
+ return Promise.resolve({
156
+ ok: true,
157
+ json: () => Promise.resolve(keyframesMock),
158
+ statusText: ''
159
+ });
160
+ };
161
+ const client = new Client(projectId, apiUrl, fetch);
162
+ const presets = await client.getKeyframes(articleId);
163
+ expect(presets).toEqual(keyframesMock);
164
+ expect(fetchCalledTimes).toEqual(1);
165
+ });
166
+
167
+ it('throws an error upon keyframes fetch failure', async () => {
168
+ const projectId = 'projectId';
169
+ const articleId = 'articleId';
170
+ const apiUrl = 'https://api.cntrl.site/';
171
+ const fetch = () => {
172
+ return Promise.resolve({
173
+ ok: false,
174
+ json: () => Promise.resolve(),
175
+ statusText: 'reason'
176
+ });
177
+ };
178
+ const client = new Client(projectId, apiUrl, fetch);
179
+ await expect(client.getKeyframes(articleId)).rejects.toEqual(
180
+ new Error(`Failed to fetch keyframes for the article with id #${articleId}: reason`)
181
+ );
182
+ });
183
+
146
184
  it('merges two meta objects into one with priority of page-based over project-based', () => {
147
185
  const pageMeta: TPageMeta = {
148
186
  enabled: true,
@@ -7,7 +7,9 @@ import {
7
7
  TPageMeta,
8
8
  TTypePresets,
9
9
  TypePresetsSchema,
10
- TPage
10
+ TPage,
11
+ TKeyframeAny,
12
+ KeyframesSchema
11
13
  } from '@cntrl-site/core';
12
14
  import fetch from 'isomorphic-fetch';
13
15
  import { URL } from 'url';
@@ -59,6 +61,13 @@ export class Client {
59
61
  return typePresets;
60
62
  }
61
63
 
64
+ async getKeyframes(articleId: string): Promise<TKeyframeAny[]> {
65
+ const response = await this.fetchKeyframes(articleId);
66
+ const data = await response.json();
67
+ const keyframes = KeyframesSchema.parse(data);
68
+ return keyframes;
69
+ }
70
+
62
71
  public static getPageMeta(projectMeta: TMeta, pageMeta: TPageMeta): TMeta {
63
72
  return pageMeta.enabled ? {
64
73
  title: pageMeta.title ? pageMeta.title : projectMeta.title,
@@ -87,6 +96,15 @@ export class Client {
87
96
  return response;
88
97
  }
89
98
 
99
+ private async fetchKeyframes(articleId: string): Promise<FetchImplResponse> {
100
+ const url = new URL(`/keyframes/${articleId}`, this.APIUrl);
101
+ const response = await this.fetchImpl(url.href);
102
+ if (!response.ok) {
103
+ throw new Error(`Failed to fetch keyframes for the article with id #${articleId}: ${response.statusText}`);
104
+ }
105
+ return response;
106
+ }
107
+
90
108
  private async fetchTypePresets(): Promise<FetchImplResponse> {
91
109
  const url = new URL(`/projects/${this.projectId}/type-presets`, this.APIUrl);
92
110
  const response = await this.fetchImpl(url.href);
@@ -0,0 +1,15 @@
1
+ import { KeyframeType, TKeyframeAny } from '@cntrl-site/core';
2
+
3
+ export const keyframesMock: TKeyframeAny[] = [
4
+ {
5
+ id: 'keyframeId',
6
+ articleId: 'articleId',
7
+ type: KeyframeType.Color,
8
+ position: 100,
9
+ value: {
10
+ color: 'black'
11
+ },
12
+ itemId: 'itemId',
13
+ layoutId: 'layoutId'
14
+ }
15
+ ];