@squiz/dx-common-lib 1.2.1-alpha.107 → 1.2.1-alpha.99

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 (108) hide show
  1. package/CHANGELOG.md +0 -64
  2. package/jest.integration.config.ts +26 -0
  3. package/lib/assertions/assertAssignWithDefaultUndefinedValue.d.ts +0 -1
  4. package/lib/assertions/assertAssignWithDefaultUndefinedValue.js +1 -3
  5. package/lib/assertions/assertAssignWithDefaultUndefinedValue.js.map +1 -1
  6. package/lib/index.d.ts +7 -4
  7. package/lib/index.js +9 -4
  8. package/lib/index.js.map +1 -1
  9. package/package.json +6 -12
  10. package/src/assertions/assertAssignWithDefaultUndefinedValue.ts +0 -5
  11. package/src/index.ts +10 -4
  12. package/tsconfig.tsbuildinfo +1 -1
  13. package/lib/assertions/assertIsEnumValue.d.ts +0 -1
  14. package/lib/assertions/assertIsEnumValue.js +0 -18
  15. package/lib/assertions/assertIsEnumValue.js.map +0 -1
  16. package/lib/assertions/assertIsEnumValue.spec.d.ts +0 -1
  17. package/lib/assertions/assertIsEnumValue.spec.js +0 -39
  18. package/lib/assertions/assertIsEnumValue.spec.js.map +0 -1
  19. package/lib/assertions/index.d.ts +0 -8
  20. package/lib/assertions/index.js +0 -27
  21. package/lib/assertions/index.js.map +0 -1
  22. package/lib/error/BadRequestError.d.ts +0 -4
  23. package/lib/error/BadRequestError.js +0 -11
  24. package/lib/error/BadRequestError.js.map +0 -1
  25. package/lib/error/ErrorWithHttpStatusCode.d.ts +0 -5
  26. package/lib/error/ErrorWithHttpStatusCode.js +0 -12
  27. package/lib/error/ErrorWithHttpStatusCode.js.map +0 -1
  28. package/lib/error/InternalServerError.d.ts +0 -4
  29. package/lib/error/InternalServerError.js +0 -11
  30. package/lib/error/InternalServerError.js.map +0 -1
  31. package/lib/error/MethodNotImplementedError.d.ts +0 -4
  32. package/lib/error/MethodNotImplementedError.js +0 -11
  33. package/lib/error/MethodNotImplementedError.js.map +0 -1
  34. package/lib/error/ResourceNotFoundError.d.ts +0 -4
  35. package/lib/error/ResourceNotFoundError.js +0 -11
  36. package/lib/error/ResourceNotFoundError.js.map +0 -1
  37. package/lib/error/TimeoutError.d.ts +0 -5
  38. package/lib/error/TimeoutError.js +0 -12
  39. package/lib/error/TimeoutError.js.map +0 -1
  40. package/lib/error/index.d.ts +0 -6
  41. package/lib/error/index.js +0 -23
  42. package/lib/error/index.js.map +0 -1
  43. package/lib/util/getNodeEnv.d.ts +0 -1
  44. package/lib/util/getNodeEnv.js +0 -14
  45. package/lib/util/getNodeEnv.js.map +0 -1
  46. package/lib/util/getNodeEnv.spec.d.ts +0 -1
  47. package/lib/util/getNodeEnv.spec.js +0 -22
  48. package/lib/util/getNodeEnv.spec.js.map +0 -1
  49. package/lib/util/getPageInfo.d.ts +0 -9
  50. package/lib/util/getPageInfo.js +0 -18
  51. package/lib/util/getPageInfo.js.map +0 -1
  52. package/lib/util/index.d.ts +0 -7
  53. package/lib/util/index.js +0 -18
  54. package/lib/util/index.js.map +0 -1
  55. package/lib/util/isPathTryingToAccessOutsideOfRoot.d.ts +0 -1
  56. package/lib/util/isPathTryingToAccessOutsideOfRoot.js +0 -14
  57. package/lib/util/isPathTryingToAccessOutsideOfRoot.js.map +0 -1
  58. package/lib/util/isPathTryingToAccessOutsideOfRoot.spec.d.ts +0 -1
  59. package/lib/util/isPathTryingToAccessOutsideOfRoot.spec.js +0 -25
  60. package/lib/util/isPathTryingToAccessOutsideOfRoot.spec.js.map +0 -1
  61. package/lib/util/isReadable.d.ts +0 -1
  62. package/lib/util/isReadable.js +0 -20
  63. package/lib/util/isReadable.js.map +0 -1
  64. package/lib/util/joinAbsoluteUrlPath.d.ts +0 -1
  65. package/lib/util/joinAbsoluteUrlPath.js +0 -26
  66. package/lib/util/joinAbsoluteUrlPath.js.map +0 -1
  67. package/lib/util/joinAbsoluteUrlPath.spec.d.ts +0 -1
  68. package/lib/util/joinAbsoluteUrlPath.spec.js +0 -43
  69. package/lib/util/joinAbsoluteUrlPath.spec.js.map +0 -1
  70. package/lib/util/never.d.ts +0 -1
  71. package/lib/util/never.js +0 -9
  72. package/lib/util/never.js.map +0 -1
  73. package/lib/util/parseEnvVarForVar.d.ts +0 -1
  74. package/lib/util/parseEnvVarForVar.js +0 -15
  75. package/lib/util/parseEnvVarForVar.js.map +0 -1
  76. package/lib/util/parseEnvVarForVar.spec.d.ts +0 -1
  77. package/lib/util/parseEnvVarForVar.spec.js +0 -20
  78. package/lib/util/parseEnvVarForVar.spec.js.map +0 -1
  79. package/lib/zip/zipDirectory.d.ts +0 -1
  80. package/lib/zip/zipDirectory.js +0 -32
  81. package/lib/zip/zipDirectory.js.map +0 -1
  82. package/lib/zip/zipDirectory.spec.d.ts +0 -1
  83. package/lib/zip/zipDirectory.spec.js +0 -25
  84. package/lib/zip/zipDirectory.spec.js.map +0 -1
  85. package/src/assertions/assertIsEnumValue.spec.ts +0 -39
  86. package/src/assertions/assertIsEnumValue.ts +0 -15
  87. package/src/assertions/index.ts +0 -11
  88. package/src/error/BadRequestError.ts +0 -7
  89. package/src/error/ErrorWithHttpStatusCode.ts +0 -5
  90. package/src/error/InternalServerError.ts +0 -7
  91. package/src/error/MethodNotImplementedError.ts +0 -7
  92. package/src/error/ResourceNotFoundError.ts +0 -7
  93. package/src/error/TimeoutError.ts +0 -8
  94. package/src/error/index.ts +0 -6
  95. package/src/util/getNodeEnv.spec.ts +0 -25
  96. package/src/util/getNodeEnv.ts +0 -11
  97. package/src/util/getPageInfo.ts +0 -23
  98. package/src/util/index.ts +0 -7
  99. package/src/util/isPathTryingToAccessOutsideOfRoot.spec.ts +0 -25
  100. package/src/util/isPathTryingToAccessOutsideOfRoot.ts +0 -8
  101. package/src/util/isReadable.ts +0 -12
  102. package/src/util/joinAbsoluteUrlPath.spec.ts +0 -62
  103. package/src/util/joinAbsoluteUrlPath.ts +0 -22
  104. package/src/util/never.ts +0 -4
  105. package/src/util/parseEnvVarForVar.spec.ts +0 -22
  106. package/src/util/parseEnvVarForVar.ts +0 -12
  107. package/src/zip/zipDirectory.spec.ts +0 -24
  108. package/src/zip/zipDirectory.ts +0 -31
@@ -1,7 +0,0 @@
1
- import { InternalServerError } from './InternalServerError';
2
-
3
- export class MethodNotImplementedError extends InternalServerError {
4
- constructor(message?: string) {
5
- super(message ?? 'method is not implemented');
6
- }
7
- }
@@ -1,7 +0,0 @@
1
- import { ErrorWithHttpStatusCode } from './ErrorWithHttpStatusCode';
2
-
3
- export class ResourceNotFoundError extends ErrorWithHttpStatusCode {
4
- constructor(message: string) {
5
- super(404, message);
6
- }
7
- }
@@ -1,8 +0,0 @@
1
- import { ErrorWithHttpStatusCode } from './ErrorWithHttpStatusCode';
2
-
3
- export class TimeoutError extends ErrorWithHttpStatusCode {
4
- name = 'TimeoutError';
5
- constructor(message: string = 'request failed to evaluate within the timeout period') {
6
- super(500, message);
7
- }
8
- }
@@ -1,6 +0,0 @@
1
- export * from './BadRequestError';
2
- export * from './ErrorWithHttpStatusCode';
3
- export * from './InternalServerError';
4
- export * from './ResourceNotFoundError';
5
- export * from './TimeoutError';
6
- export * from './MethodNotImplementedError';
@@ -1,25 +0,0 @@
1
- import { getNodeEnv } from './getNodeEnv';
2
-
3
- describe('getNodeEnv', () => {
4
- it('should return production if NODE_ENV equals production', () => {
5
- process.env.NODE_ENV = 'production';
6
- expect(getNodeEnv()).toEqual('production');
7
- });
8
-
9
- it('should return development if NODE_ENV equals development', () => {
10
- process.env.NODE_ENV = 'development';
11
- expect(getNodeEnv()).toEqual('development');
12
- });
13
-
14
- it('should return development if NODE_ENV is not set', () => {
15
- delete process.env.NODE_ENV;
16
- expect(getNodeEnv()).toEqual('development');
17
- });
18
-
19
- it('should throw if NODE_ENV set to non valid environment', () => {
20
- process.env.NODE_ENV = 'invalid';
21
- expect(() => getNodeEnv()).toThrowError(
22
- new Error('NODE_ENV env var can only be values "production" or "development"'),
23
- );
24
- });
25
- });
@@ -1,11 +0,0 @@
1
- export function getNodeEnv(): 'production' | 'development' {
2
- if (process.env.NODE_ENV == 'production') {
3
- return 'production';
4
- }
5
-
6
- if (process.env.NODE_ENV == 'development' || process.env.NODE_ENV === undefined) {
7
- return 'development';
8
- }
9
-
10
- throw new Error('NODE_ENV env var can only be values "production" or "development"');
11
- }
@@ -1,23 +0,0 @@
1
- export type PageInfoResult = {
2
- current: number;
3
- next: number | null;
4
- previous: number | null;
5
- totalPages: number;
6
- totalItems: number;
7
- pageSize: number;
8
- };
9
-
10
- export function getPageInfo(current: number, pageSize: number, totalItems: number): PageInfoResult {
11
- const totalPages = Math.ceil(totalItems / pageSize);
12
- const next = current + 1 > totalPages ? null : current + 1;
13
- const previous = current <= 1 ? null : current - 1;
14
-
15
- return {
16
- current,
17
- next,
18
- previous,
19
- totalPages,
20
- totalItems,
21
- pageSize,
22
- };
23
- }
package/src/util/index.ts DELETED
@@ -1,7 +0,0 @@
1
- export { getNodeEnv } from './getNodeEnv';
2
- export { isPathTryingToAccessOutsideOfRoot } from './isPathTryingToAccessOutsideOfRoot';
3
- export { joinAbsoluteUrlPath } from './joinAbsoluteUrlPath';
4
- export { parseEnvVarForVar } from './parseEnvVarForVar';
5
- export { isReadable } from './isReadable';
6
- export { never } from './never';
7
- export { getPageInfo, PageInfoResult } from './getPageInfo';
@@ -1,25 +0,0 @@
1
- import { isPathTryingToAccessOutsideOfRoot } from './isPathTryingToAccessOutsideOfRoot';
2
-
3
- describe('isPathTryingToAccessOutsideOfRoot', () => {
4
- it("should return false if filepath doesn't try to access outside of root", () => {
5
- expect(isPathTryingToAccessOutsideOfRoot('my-root/', 'hello')).toEqual(false);
6
- expect(isPathTryingToAccessOutsideOfRoot('my-root/', './hello')).toEqual(false);
7
- expect(isPathTryingToAccessOutsideOfRoot('my-root/', './hello/../')).toEqual(false);
8
- });
9
-
10
- it('should resolve a relative root path', () => {
11
- expect(isPathTryingToAccessOutsideOfRoot('./my-root/', 'hello')).toEqual(false);
12
- expect(isPathTryingToAccessOutsideOfRoot('./my-root/', 'hello/../world')).toEqual(false);
13
- expect(isPathTryingToAccessOutsideOfRoot('./my-root/', 'hello/world/..')).toEqual(false);
14
- expect(isPathTryingToAccessOutsideOfRoot('./my-root/', './')).toEqual(false);
15
- expect(isPathTryingToAccessOutsideOfRoot('./my-root/something', './else')).toEqual(false);
16
- expect(isPathTryingToAccessOutsideOfRoot('./my-root/something', './else/1/../../')).toEqual(false);
17
- });
18
-
19
- it('should return true if filepath does access outside of the root', () => {
20
- expect(isPathTryingToAccessOutsideOfRoot('./my-root/', '../')).toEqual(true);
21
- expect(isPathTryingToAccessOutsideOfRoot('./my-root/', '../../')).toEqual(true);
22
- expect(isPathTryingToAccessOutsideOfRoot('./my-root/', 'hello/../world/../../')).toEqual(true);
23
- expect(isPathTryingToAccessOutsideOfRoot('./my-root/', 'hello/../../')).toEqual(true);
24
- });
25
- });
@@ -1,8 +0,0 @@
1
- import path from 'path';
2
-
3
- export function isPathTryingToAccessOutsideOfRoot(rootPath: string, filePath: string) {
4
- const resolvedRoot = path.resolve(rootPath);
5
-
6
- const joinedPath = path.join(resolvedRoot, filePath);
7
- return !joinedPath.includes(resolvedRoot);
8
- }
@@ -1,12 +0,0 @@
1
- import path from 'path';
2
- import fse from 'fs-extra';
3
- import { constants } from 'fs';
4
-
5
- export async function isReadable(basePath: string, file: string): Promise<boolean> {
6
- try {
7
- await fse.access(path.join(basePath, file), constants.R_OK);
8
- return true;
9
- } catch {
10
- return false;
11
- }
12
- }
@@ -1,62 +0,0 @@
1
- import { joinAbsoluteUrlPath } from './joinAbsoluteUrlPath';
2
-
3
- describe('joinAbsoluteUrlPath', () => {
4
- it('should return a valid absolute url for protocol based inputs', () => {
5
- expect(joinAbsoluteUrlPath('https://example.com', 'aaa', 'bbb')).toEqual('https://example.com/aaa/bbb');
6
- expect(joinAbsoluteUrlPath('https://example.com/', '/aaa', 'bbb')).toEqual('https://example.com/aaa/bbb');
7
- expect(joinAbsoluteUrlPath('https://example.com/', '/aaa/', '/bbb///')).toEqual('https://example.com/aaa/bbb');
8
- expect(joinAbsoluteUrlPath('https://example.com/', './..///', '/bbb///')).toEqual('https://example.com/bbb');
9
- expect(joinAbsoluteUrlPath('https://example.com/', './aaa/', '/bbb/')).toEqual('https://example.com/aaa/bbb');
10
- expect(joinAbsoluteUrlPath('https://example.com/', '../aaa/', '/bbb')).toEqual('https://example.com/aaa/bbb');
11
-
12
- expect(joinAbsoluteUrlPath('https://example.com/', 'ccc', '../aaa/', '/bbb')).toEqual(
13
- 'https://example.com/aaa/bbb',
14
- );
15
- expect(joinAbsoluteUrlPath('https://example.com/', 'ccc', '../../aaa/', '/bbb')).toEqual(
16
- 'https://example.com/aaa/bbb',
17
- );
18
- expect(joinAbsoluteUrlPath('https://example.com/', 'ccc', '/../../aaa/', '/bbb')).toEqual(
19
- 'https://example.com/aaa/bbb',
20
- );
21
-
22
- expect(joinAbsoluteUrlPath('https://example.com/', 'ddd', 'eee', 'ccc', '/../../aaa/', '/bbb')).toEqual(
23
- 'https://example.com/ddd/aaa/bbb',
24
- );
25
- expect(joinAbsoluteUrlPath('http://localhost:3000', 'aaa', 'bbb')).toEqual('http://localhost:3000/aaa/bbb');
26
- expect(joinAbsoluteUrlPath('http://localhost:3000', '/aaa', 'bbb')).toEqual('http://localhost:3000/aaa/bbb');
27
- expect(joinAbsoluteUrlPath('http://localhost:3000', '/aaa/', '/bbb///')).toEqual('http://localhost:3000/aaa/bbb');
28
- expect(joinAbsoluteUrlPath('http://localhost:3000', './aaa/', '/bbb/')).toEqual('http://localhost:3000/aaa/bbb');
29
- expect(joinAbsoluteUrlPath('http://localhost:3000', '../aaa/', '/bbb')).toEqual('http://localhost:3000/aaa/bbb');
30
- expect(joinAbsoluteUrlPath('http://localhost:3000', 'ccc', '../aaa/', '/bbb')).toEqual(
31
- 'http://localhost:3000/aaa/bbb',
32
- );
33
- expect(joinAbsoluteUrlPath('http://localhost:3000', 'ccc', '../../aaa/', '/bbb')).toEqual(
34
- 'http://localhost:3000/aaa/bbb',
35
- );
36
- expect(joinAbsoluteUrlPath('http://localhost:3000', 'ccc', '/../../aaa/', '/bbb')).toEqual(
37
- 'http://localhost:3000/aaa/bbb',
38
- );
39
- expect(joinAbsoluteUrlPath('http://localhost:3000/', 'ddd', 'eee', 'ccc', '/../../aaa/', '/bbb')).toEqual(
40
- 'http://localhost:3000/ddd/aaa/bbb',
41
- );
42
- expect(joinAbsoluteUrlPath('http://localhost:3000/', 'localhost', 'eee', 'ccc', '/../../aaa/', '/bbb')).toEqual(
43
- 'http://localhost:3000/localhost/aaa/bbb',
44
- );
45
- });
46
-
47
- it('should return a valid resolved path for inputs', () => {
48
- expect(joinAbsoluteUrlPath('aaa', 'bbb')).toEqual('aaa/bbb');
49
- expect(joinAbsoluteUrlPath('/aaa', 'bbb')).toEqual('aaa/bbb');
50
- expect(joinAbsoluteUrlPath('/aaa/', '/bbb///')).toEqual('aaa/bbb');
51
- expect(joinAbsoluteUrlPath('./aaa/', '/bbb/')).toEqual('aaa/bbb');
52
- expect(joinAbsoluteUrlPath('../aaa/', '/bbb')).toEqual('aaa/bbb');
53
- expect(joinAbsoluteUrlPath('ccc', '../aaa/', '/bbb')).toEqual('aaa/bbb');
54
- expect(joinAbsoluteUrlPath('ccc', '../../aaa/', '/bbb')).toEqual('aaa/bbb');
55
- expect(joinAbsoluteUrlPath('ccc', '/../../aaa/', '/bbb')).toEqual('aaa/bbb');
56
- expect(joinAbsoluteUrlPath('ddd', 'eee', 'ccc', '/../../aaa/', '/bbb')).toEqual('ddd/aaa/bbb');
57
- expect(joinAbsoluteUrlPath('http', 'ccc', '/aaa/', '/bbb')).toEqual('http/ccc/aaa/bbb');
58
- expect(joinAbsoluteUrlPath('https', 'ccc', '/aaa/', '/bbb')).toEqual('https/ccc/aaa/bbb');
59
- expect(joinAbsoluteUrlPath('https', '../ccc', '/aaa/', '/bbb')).toEqual('ccc/aaa/bbb');
60
- expect(joinAbsoluteUrlPath('https', './ccc', '/aaa/', '/bbb')).toEqual('https/ccc/aaa/bbb');
61
- });
62
- });
@@ -1,22 +0,0 @@
1
- export function joinAbsoluteUrlPath(...args: string[]) {
2
- const [rootUrl] = args;
3
- /**
4
- * This method takes any form of input string and resolves them to make an absolute url
5
- * Since these paths are web paths and not file paths, we are creating a URL object (https://nodejs.org/api/url.html). http://example.com has just been used as a dummy entry to create a valid url.
6
- * Note: path.resolves gives you different output based on the OS node is running on. More reading here https://nodejs.org/api/path.html#windows-vs-posix.
7
- */
8
- const url = new URL(getSanitisedString(args), 'http://example.com');
9
- if (urlIsAbsolute(rootUrl)) {
10
- return url.toString();
11
- } else {
12
- return url.pathname.replace(/(^\/|\/$)/g, '');
13
- }
14
- }
15
-
16
- function getSanitisedString(subdirs: string[]) {
17
- return subdirs.map((dir) => dir.replace(/(^\.\/+)|(^\/+|\/+$)/g, '')).join('/');
18
- }
19
-
20
- function urlIsAbsolute(url: string) {
21
- return url.match(/^http(s?):\/\//);
22
- }
package/src/util/never.ts DELETED
@@ -1,4 +0,0 @@
1
- export function never(a: never): never {
2
- // this should never be executed. This is a compile time check
3
- throw new Error(`value ${a} is not valid`);
4
- }
@@ -1,22 +0,0 @@
1
- import { parseEnvVarForVar } from './parseEnvVarForVar';
2
-
3
- describe('parseEnvVarForVar', () => {
4
- it('it should read an environment variable from process.env.*', () => {
5
- process.env.MY_VAR = 'hello';
6
-
7
- expect(parseEnvVarForVar('MY_VAR')).toEqual('hello');
8
- });
9
-
10
- it('it should throw an error if the variable is not available', () => {
11
- expect(() => parseEnvVarForVar('DOESNT_EXIST')).toThrowError(new Error('env variable DOESNT_EXIST must be set'));
12
- });
13
-
14
- it('it should not throw an error if a default value is provided and env var doesnt exist', () => {
15
- expect(parseEnvVarForVar('DOESNT_EXIST', 'default')).toEqual('default');
16
- });
17
-
18
- it('it should return env var if its set and a defualt value is set', () => {
19
- process.env.MY_VAR = 'hello';
20
- expect(parseEnvVarForVar('MY_VAR', 'default')).toEqual('hello');
21
- });
22
- });
@@ -1,12 +0,0 @@
1
- export function parseEnvVarForVar(varName: string, defaultValue: string | undefined = undefined): string {
2
- if (process.env[varName] === undefined) {
3
- if (defaultValue !== undefined) {
4
- return defaultValue;
5
- }
6
-
7
- throw new Error(`env variable ${varName} must be set`);
8
- }
9
-
10
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
11
- return process.env[varName]!;
12
- }
@@ -1,24 +0,0 @@
1
- import path from 'path';
2
- import fs from 'fs';
3
- import { zipDirectory } from './zipDirectory';
4
-
5
- describe('zipDirectory', () => {
6
- const inputDir = fs.mkdtempSync('zip-test');
7
- const outDir = fs.mkdtempSync('zip-test-out');
8
-
9
- beforeAll(() => {
10
- fs.writeFileSync(path.join(inputDir, 'f1.txt'), '1');
11
- fs.writeFileSync(path.join(inputDir, 'f2.txt'), '2');
12
- });
13
-
14
- afterAll(() => {
15
- fs.rmSync(inputDir, { force: true, recursive: true });
16
- fs.rmSync(outDir, { force: true, recursive: true });
17
- });
18
-
19
- it('should take an input path and an output path and create a export.zip', async () => {
20
- await zipDirectory(inputDir, outDir);
21
-
22
- expect(fs.existsSync(path.join(outDir, 'export.zip'))).toEqual(true);
23
- });
24
- });
@@ -1,31 +0,0 @@
1
- import path from 'path';
2
- import fs from 'fs';
3
-
4
- import archiver from 'archiver';
5
-
6
- export function zipDirectory(sourceDirectory: string, outputDirectory: string) {
7
- return new Promise<string>((resolve, reject) => {
8
- const outputZipFile = path.join(outputDirectory, '/export.zip');
9
- const output = fs.createWriteStream(outputZipFile);
10
-
11
- const archive = archiver('zip', {
12
- zlib: { level: 9 }, // Sets the compression level.
13
- });
14
-
15
- output.on('close', function () {
16
- resolve(outputZipFile);
17
- });
18
-
19
- archive.on('warning', function (err) {
20
- reject(err);
21
- });
22
-
23
- archive.on('error', function (err) {
24
- reject(err);
25
- });
26
-
27
- archive.pipe(output);
28
- archive.directory(sourceDirectory, false);
29
- archive.finalize();
30
- });
31
- }