/
index.ts
187 lines (158 loc) · 5.79 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import {
FeatureServiceBinder,
FeatureServiceProviderDefinition,
FeatureServices,
SharedFeatureService,
} from '@feature-hub/core';
import {Logger} from '@feature-hub/logger';
import {ServerRequestV1} from '@feature-hub/server-request';
import * as history from 'history';
import {RootLocationTransformer} from './create-root-location-transformer';
import * as historyV4 from './history-v4';
import {createHistoryMultiplexers} from './internal/create-history-multiplexers';
import {createHistoryServiceV1Binder} from './internal/create-history-service-v1-binder';
import {createHistoryServiceV2Binder} from './internal/create-history-service-v2-binder';
import {
GetHistoryKeyOptions,
createHistoryServiceV3Binder,
} from './internal/create-history-service-v3-binder';
import {createHistoryServiceContext} from './internal/history-service-context';
export * from './create-root-location-transformer';
export interface RootHistory {
readonly length: number;
readonly location: RootLocation;
push(location: RootLocationDescriptorObject): void;
replace(location: RootLocationDescriptorObject): void;
createHref(location: RootLocationDescriptorObject): string;
listen(listener: historyV4.LocationListener): historyV4.UnregisterCallback;
}
export type RootLocation = historyV4.Location<ConsumerHistoryStates>;
export type RootLocationDescriptorObject =
historyV4.LocationDescriptorObject<ConsumerHistoryStates>;
export interface ConsumerHistoryStates {
readonly [historyKey: string]: unknown;
}
export interface ConsumerLocation {
readonly historyKey: string;
readonly location: historyV4.LocationDescriptorObject;
}
export interface HistoryServiceV1 {
readonly staticRootLocation: historyV4.Location;
createBrowserHistory(): historyV4.History;
createStaticHistory(): historyV4.History;
}
export interface HistoryServiceV2 {
/**
* The history key that has been assigned to the consumer. It can be used to
* create a [[ConsumerLocation]].
*/
readonly historyKey: string;
/**
* The consumer's own history. When location changes are applied to this
* history, no other consumer histories are affected.
*/
readonly history: historyV4.History;
/**
* Allows special consumers, like overarching navigation services, to change
* the full root location. To create a new root location, it is recommended to
* use the `createNewRootLocationForMultipleConsumers` method.
*/
readonly rootHistory: RootHistory;
/**
* Creates a new root location from multiple consumer locations. The returned
* location can be used for the `push`, `replace`, and `createHref` methods of
* the `rootHistory`.
*/
createNewRootLocationForMultipleConsumers(
...consumerLocations: ConsumerLocation[]
): RootLocationDescriptorObject;
}
export interface HistoryServiceV3 {
/**
* The history key that has been assigned to the consumer. It can be used to
* create a [[ConsumerLocation]].
*/
readonly historyKey: string;
/**
* The consumer's own history. When location changes are applied to this
* history, no other consumer histories are affected.
*/
readonly history: history.History;
/**
* Allows special consumers, like overarching navigation services, to change
* the full root location. To create a new root location, it is recommended to
* use the `createNewRootLocationForMultipleConsumers` method.
*/
readonly rootHistory: history.History;
/**
* Creates a new root location from multiple consumer locations. The returned
* location can be used for the `push`, `replace`, and `createHref` methods of
* the `rootHistory`. Important: For `push` and `replace` calls make sure to
* pass the returned `state` property as a second argument, e.g.:
* ```
* const {state, ...to} = historyService.createNewRootLocationForMultipleConsumers({...});
*
* historyService.rootHistory.push(to, state);
* ```
*/
createNewRootLocationForMultipleConsumers(
...consumerLocations: ConsumerLocationV3[]
): Omit<history.Location, 'key'>;
}
export interface ConsumerLocationV3 {
readonly historyKey: string;
readonly location: Partial<history.Path>;
readonly state?: unknown;
}
export interface SharedHistoryService extends SharedFeatureService {
readonly '1.0.0': FeatureServiceBinder<HistoryServiceV1>;
readonly '2.0.0': FeatureServiceBinder<HistoryServiceV2>;
readonly '3.0.0': FeatureServiceBinder<HistoryServiceV3>;
}
export interface HistoryServiceDependencies extends FeatureServices {
readonly 's2:logger'?: Logger;
readonly 's2:server-request'?: ServerRequestV1;
}
export interface HistoryServiceDefinitionOptions {
readonly mode?: 'browser' | 'static';
readonly getHistoryKey?: (options: GetHistoryKeyOptions) => string;
}
export function defineHistoryService(
rootLocationTransformer: RootLocationTransformer,
options: HistoryServiceDefinitionOptions = {},
): FeatureServiceProviderDefinition<
SharedHistoryService,
HistoryServiceDependencies
> {
const {mode = 'browser', getHistoryKey = ({consumerId}) => consumerId} =
options;
return {
id: 's2:history',
optionalDependencies: {
featureServices: {
's2:logger': '^1.0.0',
's2:server-request': '^1.0.0',
},
},
create: (env) => {
const context = createHistoryServiceContext(env.featureServices);
const historyMultiplexers = createHistoryMultiplexers(
context,
rootLocationTransformer,
);
return {
'1.0.0': createHistoryServiceV1Binder(context, historyMultiplexers, {
getHistoryKey,
}),
'2.0.0': createHistoryServiceV2Binder(context, historyMultiplexers, {
mode,
getHistoryKey,
}),
'3.0.0': createHistoryServiceV3Binder(context, historyMultiplexers, {
mode,
getHistoryKey,
}),
};
},
};
}