@vroskus/library-history 1.0.0 → 1.0.2

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/.eslintrc ADDED
@@ -0,0 +1,77 @@
1
+ {
2
+ "root": true,
3
+ "parser": "@typescript-eslint/parser",
4
+ "parserOptions": {
5
+ "requireConfigFile": false,
6
+ "project": ["tsconfig.json"]
7
+ },
8
+ "extends": [
9
+ "airbnb-base",
10
+ "airbnb-typescript",
11
+ "plugin:import/recommended",
12
+ "plugin:@typescript-eslint/eslint-recommended",
13
+ "plugin:@typescript-eslint/recommended",
14
+ "plugin:sort/recommended"
15
+ ],
16
+ "plugins": [
17
+ "@typescript-eslint",
18
+ "import",
19
+ "import-newlines",
20
+ "react",
21
+ "sort"
22
+ ],
23
+ "rules": {
24
+ "complexity": ["error", 5],
25
+ "@typescript-eslint/ban-ts-comment": [1],
26
+ "@typescript-eslint/no-unused-vars": [2],
27
+ "curly": ["error", "all"],
28
+ "import/no-cycle": [0],
29
+ "no-await-in-loop": 0,
30
+ "no-console": "error",
31
+ "no-duplicate-imports": [0],
32
+ "no-loss-of-precision": [0],
33
+ "no-restricted-syntax": 0,
34
+ "no-unreachable-loop": [0],
35
+ "no-useless-backreference": [0],
36
+ "no-unused-private-class-members": [2],
37
+ "padding-line-between-statements": ["error", {
38
+ "blankLine": "always",
39
+ "prev": ["const", "let"],
40
+ "next": "*"
41
+ }, {
42
+ "blankLine": "any",
43
+ "prev": ["const", "let"],
44
+ "next": ["const", "let"]
45
+ }, {
46
+ "blankLine": "always",
47
+ "prev": ["if", "function", "for"],
48
+ "next": "*"
49
+ }, {
50
+ "blankLine": "always",
51
+ "prev": "*",
52
+ "next": "return"
53
+ }],
54
+ "object-curly-newline": ["error", {
55
+ "ObjectExpression": "always",
56
+ "ObjectPattern": "always",
57
+ "ImportDeclaration": "always",
58
+ "ExportDeclaration": "always"
59
+ }],
60
+ "function-call-argument-newline": ["error", "always"],
61
+ "object-property-newline": ["error", {
62
+ "allowMultiplePropertiesPerLine": false
63
+ }],
64
+ "import-newlines/enforce": ["error", {
65
+ "items": 1,
66
+ "forceSingleLine": false
67
+ }],
68
+ "sort/imports": [0],
69
+ "sort/exports": [0]
70
+ },
71
+ "env": {
72
+ "browser": true,
73
+ "node": true,
74
+ "jest": true,
75
+ "es6": true
76
+ }
77
+ }
@@ -0,0 +1,17 @@
1
+ import type { $Location } from '@vroskus/library-types';
2
+ type $Listener = (location: $Location) => unknown;
3
+ export type $RedirectParams = {
4
+ hash?: string;
5
+ pathname?: string;
6
+ search?: string;
7
+ state?: object;
8
+ };
9
+ export type $RedirectResponse = $RedirectParams;
10
+ export type $HistoryService = {
11
+ readonly addListener: (listener: $Listener) => number;
12
+ readonly getCurrentPathname: () => string | null;
13
+ readonly redirect: (arg0: $RedirectParams) => $RedirectResponse;
14
+ readonly render: unknown;
15
+ };
16
+ declare const HistoryService: $HistoryService;
17
+ export default HistoryService;
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAQA,6CAA+B;AAC/B,uDAE0B;AAO1B,IAAI,aAAa,GAAG,IAAI,CAAC;AACzB,MAAM,SAAS,GAAqB,EAAE,CAAC;AAEvC,MAAM,GAAI,SAAQ,KAAK,CAAC,SAAiB;IACvC,iBAAiB;QACf,IAAI,CAAC,kBAAkB,EAAE,CAAC;IAC5B,CAAC;IAED,kBAAkB;QAChB,IAAI,CAAC,kBAAkB,EAAE,CAAC;IAC5B,CAAC;IAED,kBAAkB;QAChB,MAAM,EACJ,OAAO,GACR,GAAG,IAAI,CAAC,KAAK,CAAC;QAEf,SAAS,CAAC,OAAO,CAAC,CAAC,QAAmB,EAAE,EAAE;YACxC,QAAQ,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;QAC7B,CAAC,CAAC,CAAC;QAEH,aAAa,GAAG,OAAO,CAAC;IAC1B,CAAC;IAED,MAAM;QACJ,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AAiBD,MAAM,cAAc,GAAoB;IACtC,WAAW,EAAE,CAAC,QAAmB,EAAE,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC;IAC9D,kBAAkB,EAAE,GAAG,EAAE;QACvB,IAAI,MAAM,IAAI,MAAM,CAAC,QAAQ,EAAE;YAC7B,OAAO,MAAM,CAAC,QAAQ,CAAC,QAAQ,IAAI,IAAI,CAAC;SACzC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;IACD,QAAQ,EAAE,CAAC,MAAuB,EAAqB,EAAE;QACvD,IAAI,aAAa,EAAE;YACjB,aAAa,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;SAC5B;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;IACD,MAAM,EAAE,IAAA,6BAAU,EAAC,GAAG,CAAY;CACnC,CAAC;AAEF,kBAAe,cAAc,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vroskus/library-history",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "History",
5
5
  "author": "Vilius Roškus <vilius@regattas.eu>",
6
6
  "license": "MIT",
@@ -9,9 +9,6 @@
9
9
  "url": "git+https://github.com/vroskus/library-history.git"
10
10
  },
11
11
  "main": "dist/index.js",
12
- "files": [
13
- "dist/index.js"
14
- ],
15
12
  "scripts": {
16
13
  "postinstall": "npm run build",
17
14
  "build": "tsc",
@@ -20,23 +17,23 @@
20
17
  "test:e2e": "echo 'No tests'"
21
18
  },
22
19
  "dependencies": {
23
- "@vroskus/library-types": "1.0.1",
20
+ "@vroskus/library-types": "1.0.3",
24
21
  "react": "18.2.0",
25
22
  "react-router-dom": "5.3.0"
26
23
  },
27
24
  "devDependencies": {
28
25
  "@types/jest": "29.5.0",
29
26
  "@types/node": "18.15.11",
30
- "@types/react": "18.0.33",
31
- "@typescript-eslint/eslint-plugin": "5.53.0",
32
- "@typescript-eslint/parser": "5.53.0",
33
- "eslint": "8.34.0",
27
+ "@types/react": "18.0.34",
28
+ "@typescript-eslint/eslint-plugin": "5.58.0",
29
+ "@typescript-eslint/parser": "5.58.0",
30
+ "eslint": "8.38.0",
34
31
  "eslint-config-airbnb-base": "15.0.0",
35
32
  "eslint-config-airbnb-typescript": "17.0.0",
36
33
  "eslint-plugin-import": "2.27.5",
37
34
  "eslint-plugin-import-newlines": "1.3.1",
38
35
  "eslint-plugin-react": "7.32.2",
39
- "eslint-plugin-sort": "2.4.0",
36
+ "eslint-plugin-sort": "2.7.1",
40
37
  "npm-check": "6.0.1",
41
38
  "typescript": "4.9.5"
42
39
  }
package/src/index.ts ADDED
@@ -0,0 +1,82 @@
1
+ /* eslint-disable class-methods-use-this */
2
+ // Global Types
3
+ import type {
4
+ $Component,
5
+ $Location,
6
+ $WebRouter,
7
+ } from '@vroskus/library-types';
8
+
9
+ import * as React from 'react';
10
+ import {
11
+ withRouter,
12
+ } from 'react-router-dom';
13
+
14
+ // Types
15
+ type $Listener = (location: $Location) => unknown;
16
+
17
+ type $Props = $WebRouter;
18
+
19
+ let globalHistory = null;
20
+ const listeners: Array<$Listener> = [];
21
+
22
+ class Spy extends React.Component<$Props> {
23
+ componentDidMount(): void {
24
+ this.trackHistoryChange();
25
+ }
26
+
27
+ componentDidUpdate(): void {
28
+ this.trackHistoryChange();
29
+ }
30
+
31
+ trackHistoryChange(): void {
32
+ const {
33
+ history,
34
+ } = this.props;
35
+
36
+ listeners.forEach((listener: $Listener) => {
37
+ listener(history.location);
38
+ });
39
+
40
+ globalHistory = history;
41
+ }
42
+
43
+ render(): null {
44
+ return null;
45
+ }
46
+ }
47
+
48
+ export type $RedirectParams = {
49
+ hash?: string;
50
+ pathname?: string;
51
+ search?: string;
52
+ state?: object;
53
+ };
54
+ export type $RedirectResponse = $RedirectParams;
55
+
56
+ export type $HistoryService = {
57
+ readonly addListener: (listener: $Listener) => number;
58
+ readonly getCurrentPathname: () => string | null;
59
+ readonly redirect: (arg0: $RedirectParams) => $RedirectResponse;
60
+ readonly render: unknown;
61
+ };
62
+
63
+ const HistoryService: $HistoryService = {
64
+ addListener: (listener: $Listener) => listeners.push(listener),
65
+ getCurrentPathname: () => {
66
+ if (window && window.location) {
67
+ return window.location.pathname || null;
68
+ }
69
+
70
+ return null;
71
+ },
72
+ redirect: (params: $RedirectParams): $RedirectResponse => {
73
+ if (globalHistory) {
74
+ globalHistory.push(params);
75
+ }
76
+
77
+ return params;
78
+ },
79
+ render: withRouter(Spy) as unknown,
80
+ };
81
+
82
+ export default HistoryService;
package/tsconfig.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "compilerOptions": {
3
+ "outDir": "./dist/",
4
+ "types": ["node", "jest"],
5
+ "module": "commonjs",
6
+ "esModuleInterop": true,
7
+ "declaration": true,
8
+ "noImplicitAny": false,
9
+ "removeComments": true,
10
+ "noLib": false,
11
+ "emitDecoratorMetadata": true,
12
+ "experimentalDecorators": true,
13
+ "target": "es6",
14
+ "sourceMap": true,
15
+ "lib": [
16
+ "dom",
17
+ "es6"
18
+ ],
19
+ "jsx": "react"
20
+ },
21
+ "exclude": [
22
+ "node_modules",
23
+ "./dist/**/*"
24
+ ],
25
+ "include": [
26
+ "./src"
27
+ ]
28
+ }