@cntrl-site/sdk 0.0.6 → 0.2.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.
@@ -11,6 +11,12 @@ class Client {
11
11
  this.projectId = projectId;
12
12
  this.APIUrl = APIUrl;
13
13
  this.fetchImpl = fetchImpl;
14
+ if (projectId.length === 0) {
15
+ throw new Error('CNTRL SDK: Project ID is empty. Did you forget to pass it?');
16
+ }
17
+ if (APIUrl.length === 0) {
18
+ throw new Error('CNTRL SDK: API URL is empty. Did you forget to pass it?');
19
+ }
14
20
  }
15
21
  async getProject() {
16
22
  const response = await this.fetchImpl(`${this.APIUrl}/projects/${this.projectId}`);
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FontFaceGenerator = void 0;
4
+ const FILE_TYPES_MAP = {
5
+ ttf: 'truetype'
6
+ };
7
+ class FontFaceGenerator {
8
+ constructor(fonts) {
9
+ this.fonts = fonts;
10
+ }
11
+ generate() {
12
+ return this.fonts.map(font => {
13
+ const eotFile = font.files.find(file => file.type === 'eot');
14
+ const otherFiles = font.files
15
+ .filter(file => file.type !== 'eot')
16
+ .map(file => `url('${file.url}') format('${FILE_TYPES_MAP[file.type] || file.type}')`);
17
+ return `
18
+ @font-face {
19
+ font-family: ${font.name};
20
+ font-weight: ${font.weight};
21
+ ${eotFile ? `src: url('${eotFile.url}');\n ` : ''}src: ${otherFiles.join(', ')};
22
+ }`;
23
+ }).join('\n');
24
+ }
25
+ }
26
+ exports.FontFaceGenerator = FontFaceGenerator;
package/lib/index.js CHANGED
@@ -1,7 +1,24 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
2
16
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getLayoutStyles = exports.CntrlClient = void 0;
4
- var Client_1 = require("./client/Client");
17
+ exports.getLayoutStyles = exports.FontFaceGenerator = exports.CntrlClient = void 0;
18
+ var Client_1 = require("./Client/Client");
5
19
  Object.defineProperty(exports, "CntrlClient", { enumerable: true, get: function () { return Client_1.Client; } });
20
+ var FontFaceGenerator_1 = require("./FontFaceGenerator/FontFaceGenerator");
21
+ Object.defineProperty(exports, "FontFaceGenerator", { enumerable: true, get: function () { return FontFaceGenerator_1.FontFaceGenerator; } });
6
22
  var utils_1 = require("./utils");
7
23
  Object.defineProperty(exports, "getLayoutStyles", { enumerable: true, get: function () { return utils_1.getLayoutStyles; } });
24
+ __exportStar(require("@cntrl-site/core"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cntrl-site/sdk",
3
- "version": "0.0.6",
3
+ "version": "0.2.0",
4
4
  "description": "Generic SDK for use in public websites.",
5
5
  "main": "lib/index.js",
6
6
  "types": "src/index.ts",
@@ -24,13 +24,16 @@
24
24
  "lib": "lib"
25
25
  },
26
26
  "dependencies": {
27
- "@cntrl-site/core": "^1.0.9",
27
+ "@cntrl-site/core": "^1.0.12",
28
28
  "@types/isomorphic-fetch": "^0.0.36",
29
29
  "isomorphic-fetch": "^3.0.0"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@tsconfig/node16": "^1.0.3",
33
33
  "@tsconfig/recommended": "^1.0.1",
34
+ "@types/jest": "^29.0.0",
35
+ "jest": "^28.1.3",
36
+ "ts-jest": "^28.0.8",
34
37
  "typescript": "^4.7.4"
35
38
  }
36
39
  }
@@ -6,7 +6,14 @@ export class Client {
6
6
  private projectId: string,
7
7
  private APIUrl: string,
8
8
  private fetchImpl = fetch
9
- ) {}
9
+ ) {
10
+ if (projectId.length === 0) {
11
+ throw new Error('CNTRL SDK: Project ID is empty. Did you forget to pass it?');
12
+ }
13
+ if (APIUrl.length === 0) {
14
+ throw new Error('CNTRL SDK: API URL is empty. Did you forget to pass it?');
15
+ }
16
+ }
10
17
 
11
18
  async getProject(): Promise<TProject> {
12
19
  const response = await this.fetchImpl(`${this.APIUrl}/projects/${this.projectId}`);
@@ -0,0 +1,53 @@
1
+ import { FontFaceGenerator } from './FontFaceGenerator';
2
+ import { FontFileTypes, TCustomFont } from '@cntrl-site/core';
3
+
4
+ describe('FontFaceGenerator', () => {
5
+ it('generates font face with eot', () => {
6
+ const fonts: TCustomFont[] = [
7
+ {
8
+ name: 'Aeonik',
9
+ weight: 400,
10
+ style: 'normal',
11
+ files: [
12
+ {
13
+ type: FontFileTypes.EOT,
14
+ url: 'link/to/font.eot'
15
+ },
16
+ {
17
+ type: FontFileTypes.WOFF,
18
+ url: 'link/to/font.woff'
19
+ }
20
+ ]
21
+ },
22
+ {
23
+ name: 'Anek Odia',
24
+ weight: 700,
25
+ style: 'italic',
26
+ files: [
27
+ {
28
+ type: FontFileTypes.WOFF,
29
+ url: 'link/to/font.woff'
30
+ },
31
+ {
32
+ type: FontFileTypes.TTF,
33
+ url: 'link/to/font.ttf'
34
+ }
35
+ ]
36
+ }
37
+ ];
38
+ const generator = new FontFaceGenerator(fonts);
39
+ expect(generator.generate()).toEqual(`
40
+ @font-face {
41
+ font-family: Aeonik;
42
+ font-weight: 400;
43
+ src: url('link/to/font.eot');
44
+ src: url('link/to/font.woff') format('woff');
45
+ }
46
+
47
+ @font-face {
48
+ font-family: Anek Odia;
49
+ font-weight: 700;
50
+ src: url('link/to/font.woff') format('woff'), url('link/to/font.ttf') format('truetype');
51
+ }`)
52
+ });
53
+ });
@@ -0,0 +1,26 @@
1
+ import { TCustomFont } from '@cntrl-site/core';
2
+
3
+ const FILE_TYPES_MAP: Record<string, string> = {
4
+ ttf: 'truetype'
5
+ };
6
+
7
+ export class FontFaceGenerator {
8
+ constructor(
9
+ private fonts: TCustomFont[]
10
+ ) {}
11
+
12
+ generate(): string {
13
+ return this.fonts.map(font => {
14
+ const eotFile = font.files.find(file => file.type === 'eot');
15
+ const otherFiles = font.files
16
+ .filter(file => file.type !== 'eot')
17
+ .map(file => `url('${file.url}') format('${FILE_TYPES_MAP[file.type] || file.type}')`);
18
+ return `
19
+ @font-face {
20
+ font-family: ${font.name};
21
+ font-weight: ${font.weight};
22
+ ${eotFile ? `src: url('${eotFile.url}');\n ` : ''}src: ${otherFiles.join(', ')};
23
+ }`;
24
+ }).join('\n');
25
+ }
26
+ }
package/src/index.ts CHANGED
@@ -1,2 +1,4 @@
1
- export { Client as CntrlClient } from './client/Client';
1
+ export { Client as CntrlClient } from './Client/Client';
2
+ export { FontFaceGenerator } from './FontFaceGenerator/FontFaceGenerator';
2
3
  export { getLayoutStyles } from './utils';
4
+ export * from '@cntrl-site/core';
package/src/utils.ts CHANGED
@@ -19,3 +19,4 @@ export function getLayoutStyles<V, M> (
19
19
  '');
20
20
  return mediaQueries;
21
21
  }
22
+