@thoughtbot/superglue 1.0.2 → 2.0.0-alpha.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.
@@ -43,17 +43,17 @@ interface RemoteProps extends BaseProps {
43
43
  force?: boolean;
44
44
  }
45
45
  interface BeforeSave {
46
- (prevPage: VisitResponse, receivedPage: VisitResponse): VisitResponse;
46
+ <T extends PageResponse>(prevPage: SaveResponse, receivedPage: T): T;
47
47
  }
48
48
  interface ApplicationRemote {
49
- (input: string | PageKey, options: RemoteProps & {
49
+ (input: string | PageKey, options?: RemoteProps & {
50
50
  dataset?: {
51
51
  [name: string]: string | undefined;
52
52
  };
53
53
  }): Promise<Meta>;
54
54
  }
55
55
  interface ApplicationVisit {
56
- (input: string | PageKey, options: VisitProps & {
56
+ (input: string | PageKey, options?: VisitProps & {
57
57
  dataset?: {
58
58
  [name: string]: string | undefined;
59
59
  };
@@ -62,9 +62,11 @@ interface ApplicationVisit {
62
62
 
63
63
  declare function pageReducer(state: AllPages | undefined, action: Action): AllPages;
64
64
  declare function superglueReducer(state: SuperglueState | undefined, action: Action): SuperglueState;
65
+ declare function fragmentReducer(state: AllFragments | undefined, action: Action): AllFragments;
65
66
  declare const rootReducer: {
66
67
  superglue: typeof superglueReducer;
67
68
  pages: typeof pageReducer;
69
+ fragments: typeof fragmentReducer;
68
70
  };
69
71
 
70
72
  type PageKey = string;
@@ -80,6 +82,16 @@ type JSONObject = {
80
82
  type JSONMappable = JSONValue[] | JSONObject;
81
83
  type JSONKeyable = JSONObject[] | JSONObject;
82
84
  type JSONValue = JSONPrimitive | JSONMappable;
85
+ type Fragment<T> = {
86
+ __id: string;
87
+ } & {
88
+ __fragmentType?: T;
89
+ };
90
+ type Unproxied<T> = T extends Fragment<unknown> ? {
91
+ __id: string;
92
+ } : T extends (infer U)[] ? Unproxied<U>[] : T extends object ? {
93
+ [K in keyof T]: Unproxied<T[K]>;
94
+ } : T;
83
95
  interface ParsedResponse {
84
96
  rsp: Response;
85
97
  json: PageResponse;
@@ -91,30 +103,57 @@ type Defer = {
91
103
  successAction: string;
92
104
  failAction: string;
93
105
  };
94
- type VisitResponse<T = JSONMappable> = {
106
+ type SaveResponse<T = JSONMappable> = {
95
107
  data: T;
96
108
  componentIdentifier: ComponentIdentifier;
97
109
  assets: string[];
98
110
  csrfToken?: string;
99
- fragments: Fragment[];
111
+ fragments: FragmentRef[];
100
112
  defers: Defer[];
101
113
  slices: JSONObject;
114
+ action: 'savePage';
102
115
  renderedAt: number;
103
116
  restoreStrategy: RestoreStrategy;
104
117
  };
105
- type Page<T = JSONMappable> = VisitResponse<T> & {
118
+ type Page<T = JSONMappable> = SaveResponse<T> & {
106
119
  savedAt: number;
107
120
  };
108
- type GraftResponse<T = JSONMappable> = VisitResponse<T> & {
121
+ type GraftResponse<T = JSONMappable> = {
122
+ data: T;
123
+ componentIdentifier: ComponentIdentifier;
124
+ assets: string[];
125
+ csrfToken?: string;
126
+ fragments: FragmentRef[];
127
+ defers: Defer[];
128
+ slices: JSONObject;
109
129
  action: 'graft';
130
+ renderedAt: number;
131
+ restoreStrategy: RestoreStrategy;
110
132
  path: Keypath;
133
+ fragmentContext?: string;
134
+ };
135
+ type StreamMutateMessage = {
136
+ data: JSONMappable;
137
+ fragmentIds: string[];
138
+ handler: 'append' | 'prepend' | 'save';
139
+ options: Record<string, string>;
140
+ };
141
+ type StreamResponse = {
142
+ data: StreamMutateMessage[];
143
+ fragments: FragmentRef[];
144
+ assets: string[];
145
+ csrfToken?: string;
146
+ action: 'handleStreamResponse';
147
+ renderedAt: number;
148
+ slices: JSONObject;
111
149
  };
112
- type PageResponse = GraftResponse | VisitResponse;
113
- type Fragment = {
114
- type: string;
150
+ type PageResponse = GraftResponse | SaveResponse | StreamResponse;
151
+ type FragmentRef = {
152
+ id: string;
115
153
  path: Keypath;
116
154
  };
117
155
  type AllPages<T = JSONMappable> = Record<PageKey, Page<T>>;
156
+ type AllFragments = Record<string, JSONMappable>;
118
157
  interface SuperglueState {
119
158
  currentPageKey: PageKey;
120
159
  search: Record<string, string | undefined>;
@@ -124,15 +163,16 @@ interface SuperglueState {
124
163
  interface RootState<T = JSONMappable> {
125
164
  superglue: SuperglueState;
126
165
  pages: AllPages<T>;
166
+ fragments: AllFragments;
127
167
  [name: string]: unknown;
128
168
  }
129
169
  interface Meta {
130
170
  pageKey: PageKey;
131
- page: VisitResponse;
171
+ page: PageResponse;
132
172
  redirected: boolean;
133
173
  rsp: Response;
134
174
  fetchArgs: FetchArgs;
135
- componentIdentifier: ComponentIdentifier;
175
+ componentIdentifier?: ComponentIdentifier;
136
176
  needsRefresh: boolean;
137
177
  }
138
178
  interface VisitMeta extends Meta {
@@ -201,7 +241,7 @@ interface BuildVisitAndRemote {
201
241
  };
202
242
  }
203
243
  interface SetupProps {
204
- initialPage: VisitResponse;
244
+ initialPage: SaveResponse;
205
245
  baseUrl: string;
206
246
  path: string;
207
247
  store: SuperglueStore;
@@ -212,7 +252,7 @@ interface SetupProps {
212
252
  } | null>;
213
253
  }
214
254
  interface ApplicationProps extends React.ComponentPropsWithoutRef<'div'> {
215
- initialPage: VisitResponse;
255
+ initialPage: SaveResponse;
216
256
  baseUrl: string;
217
257
  path: string;
218
258
  buildVisitAndRemote: BuildVisitAndRemote;
@@ -227,6 +267,6 @@ declare class MismatchedComponentError extends Error {
227
267
  declare const remote: RemoteCreator;
228
268
  declare const visit: VisitCreator;
229
269
 
230
- declare function saveAndProcessPage(pageKey: string, page: VisitResponse | GraftResponse): SaveAndProcessPageThunk;
270
+ declare function saveAndProcessPage(pageKey: string, page: PageResponse): SaveAndProcessPageThunk;
231
271
 
232
- export { type GraftingSuccessAction as $, type ApplicationVisit as A, type SaveAndProcessPageThunk as B, type ComponentIdentifier as C, type Defer as D, type MetaThunk as E, type FetchArgs as F, type GraftResponse as G, type Handlers as H, type VisitMetaThunk as I, type JSONMappable as J, type Keypath as K, type DefermentThunk as L, type Meta as M, type NavigationContextProps as N, type BasicRequestInit as O, type PageKey as P, type BuildStore as Q, type RestoreStrategy as R, type SuperglueState as S, type BuildVisitAndRemote as T, type UJSHandlers as U, type VisitResponse as V, type Visit as W, type VisitProps as X, type Remote as Y, type RemoteProps as Z, type BeforeSave as _, type JSONValue as a, type GraftingErrorAction as a0, MismatchedComponentError as a1, remote as a2, visit as a3, type NavigationProviderProps as b, type NavigateTo as c, type SuperglueStore as d, type SetupProps as e, type ApplicationRemote as f, type ApplicationProps as g, superglueReducer as h, type NavigationAction as i, type JSONPrimitive as j, type JSONObject as k, type JSONKeyable as l, type ParsedResponse as m, type Page as n, type PageResponse as o, pageReducer as p, type Fragment as q, rootReducer as r, saveAndProcessPage as s, type AllPages as t, type RootState as u, type VisitMeta as v, type VisitCreator as w, type RemoteCreator as x, type Dispatch as y, type HistoryState as z };
272
+ export { type Visit as $, type ApplicationRemote as A, type VisitCreator as B, type ComponentIdentifier as C, type Defer as D, type RemoteCreator as E, type FetchArgs as F, type GraftResponse as G, type Handlers as H, type Dispatch as I, type JSONMappable as J, type Keypath as K, type UJSHandlers as L, type Meta as M, type NavigationContextProps as N, type HistoryState as O, type PageKey as P, type SaveAndProcessPageThunk as Q, type RestoreStrategy as R, type SuperglueStore as S, type MetaThunk as T, type Unproxied as U, type VisitMeta as V, type VisitMetaThunk as W, type DefermentThunk as X, type BasicRequestInit as Y, type BuildStore as Z, type BuildVisitAndRemote as _, type JSONValue as a, type VisitProps as a0, type Remote as a1, type RemoteProps as a2, type BeforeSave as a3, type GraftingSuccessAction as a4, type GraftingErrorAction as a5, MismatchedComponentError as a6, remote as a7, visit as a8, type NavigationProviderProps as b, type NavigateTo as c, type SaveResponse as d, type PageResponse as e, type SuperglueState as f, type SetupProps as g, type ApplicationVisit as h, type ApplicationProps as i, superglueReducer as j, type NavigationAction as k, type JSONPrimitive as l, type JSONObject as m, type JSONKeyable as n, type Fragment as o, pageReducer as p, type ParsedResponse as q, rootReducer as r, saveAndProcessPage as s, type Page as t, type StreamMutateMessage as u, type StreamResponse as v, type FragmentRef as w, type AllPages as x, type AllFragments as y, type RootState as z };
@@ -1,13 +1,39 @@
1
1
  import * as history from 'history';
2
- import { J as JSONMappable, K as Keypath, a as JSONValue, P as PageKey, N as NavigationContextProps, b as NavigationProviderProps, c as NavigateTo, V as VisitResponse, F as FetchArgs, S as SuperglueState, d as SuperglueStore, e as SetupProps, A as ApplicationVisit, f as ApplicationRemote, H as Handlers, g as ApplicationProps } from './index-BYr1PoYr.mjs';
3
- export { t as AllPages, O as BasicRequestInit, _ as BeforeSave, Q as BuildStore, T as BuildVisitAndRemote, C as ComponentIdentifier, D as Defer, L as DefermentThunk, y as Dispatch, q as Fragment, G as GraftResponse, a0 as GraftingErrorAction, $ as GraftingSuccessAction, z as HistoryState, l as JSONKeyable, k as JSONObject, j as JSONPrimitive, M as Meta, E as MetaThunk, i as NavigationAction, n as Page, o as PageResponse, m as ParsedResponse, Y as Remote, x as RemoteCreator, Z as RemoteProps, R as RestoreStrategy, u as RootState, B as SaveAndProcessPageThunk, U as UJSHandlers, W as Visit, w as VisitCreator, v as VisitMeta, I as VisitMetaThunk, X as VisitProps, p as pageReducer, r as rootReducer, s as saveAndProcessPage, h as superglueReducer } from './index-BYr1PoYr.mjs';
2
+ import { J as JSONMappable, K as Keypath, a as JSONValue, P as PageKey, A as ApplicationRemote, S as SuperglueStore, N as NavigationContextProps, b as NavigationProviderProps, c as NavigateTo, d as SaveResponse, F as FetchArgs, e as PageResponse, U as Unproxied, f as SuperglueState, g as SetupProps, h as ApplicationVisit, H as Handlers, i as ApplicationProps } from './index-O_srQ3Nm.mjs';
3
+ export { y as AllFragments, x as AllPages, Y as BasicRequestInit, a3 as BeforeSave, Z as BuildStore, _ as BuildVisitAndRemote, C as ComponentIdentifier, D as Defer, X as DefermentThunk, I as Dispatch, o as Fragment, w as FragmentRef, G as GraftResponse, a5 as GraftingErrorAction, a4 as GraftingSuccessAction, O as HistoryState, n as JSONKeyable, m as JSONObject, l as JSONPrimitive, M as Meta, T as MetaThunk, k as NavigationAction, t as Page, q as ParsedResponse, a1 as Remote, E as RemoteCreator, a2 as RemoteProps, R as RestoreStrategy, z as RootState, Q as SaveAndProcessPageThunk, u as StreamMutateMessage, v as StreamResponse, L as UJSHandlers, $ as Visit, B as VisitCreator, V as VisitMeta, W as VisitMetaThunk, a0 as VisitProps, p as pageReducer, r as rootReducer, s as saveAndProcessPage, j as superglueReducer } from './index-O_srQ3Nm.mjs';
4
4
  import React from 'react';
5
+ import { Subscription, Consumer, ChannelNameWithParams } from '@rails/actioncable';
6
+ import { DebouncedFunc } from 'lodash';
5
7
  import * as _reduxjs_toolkit from '@reduxjs/toolkit';
6
8
 
7
9
  declare function getIn(node: JSONMappable, path: Keypath): JSONValue;
8
10
 
9
11
  declare function urlToPageKey(url: string): PageKey;
10
12
 
13
+ type StreamSourceProps = string | ChannelNameWithParams;
14
+ declare class StreamActions {
15
+ attributePrefix: string;
16
+ remote: DebouncedFunc<ApplicationRemote>;
17
+ private store;
18
+ constructor({ remote, store, }: {
19
+ remote: ApplicationRemote;
20
+ store: SuperglueStore;
21
+ });
22
+ refresh(pageKey: string): void;
23
+ prepend(fragments: string[], data: JSONMappable, options?: {
24
+ saveAs?: string;
25
+ }): void;
26
+ save(fragment: string, data: JSONMappable): void;
27
+ append(fragments: string[], data: JSONMappable, options?: {
28
+ saveAs?: string;
29
+ }): void;
30
+ handle(rawMessage: string, currentPageKey: string): void;
31
+ }
32
+ declare function useStreamSource(channel: StreamSourceProps): {
33
+ connected: boolean;
34
+ subscription: Subscription<Consumer> | null;
35
+ };
36
+
11
37
  declare const NavigationContext: React.Context<NavigationContextProps>;
12
38
  declare const NavigationProvider: React.ForwardRefExoticComponent<NavigationProviderProps & React.RefAttributes<{
13
39
  navigateTo: NavigateTo | null;
@@ -17,18 +43,11 @@ declare const GRAFTING_ERROR = "@@superglue/GRAFTING_ERROR";
17
43
  declare const GRAFTING_SUCCESS = "@@superglue/GRAFTING_SUCCESS";
18
44
  declare const saveResponse: _reduxjs_toolkit.ActionCreatorWithPreparedPayload<[{
19
45
  pageKey: string;
20
- page: VisitResponse;
46
+ page: SaveResponse;
21
47
  }], {
22
48
  pageKey: string;
23
- page: VisitResponse;
49
+ page: SaveResponse;
24
50
  }, "@@superglue/SAVE_RESPONSE", never, never>;
25
- declare const updateFragments: _reduxjs_toolkit.ActionCreatorWithPayload<{
26
- name: string;
27
- path: Keypath;
28
- pageKey: PageKey;
29
- value: JSONMappable;
30
- previousValue?: JSONMappable;
31
- }, string>;
32
51
  declare const copyPage: _reduxjs_toolkit.ActionCreatorWithPayload<{
33
52
  from: PageKey;
34
53
  to: PageKey;
@@ -47,18 +66,47 @@ declare const beforeRemote: _reduxjs_toolkit.ActionCreatorWithPayload<{
47
66
  currentPageKey: PageKey;
48
67
  fetchArgs: FetchArgs;
49
68
  }, string>;
69
+ declare const receiveResponse: _reduxjs_toolkit.ActionCreatorWithPreparedPayload<[{
70
+ pageKey: string;
71
+ response: PageResponse;
72
+ }], {
73
+ pageKey: string;
74
+ response: PageResponse;
75
+ }, "@@superglue/RECEIVE_RESPONSE", never, never>;
76
+
77
+ type ProxiedContent<T> = T & {
78
+ readonly [K in keyof T]: T[K] extends {
79
+ __id: string;
80
+ } ? unknown : T[K] extends (infer U)[] ? ProxiedContent<U>[] : T[K] extends object ? ProxiedContent<T[K]> : T[K];
81
+ };
82
+ type FragmentRef = {
83
+ __id: string;
84
+ } | string;
85
+ declare function useContent<T = JSONMappable>(): ProxiedContent<T>;
86
+ declare function useContent<T = JSONMappable>(fragmentRef: FragmentRef): ProxiedContent<T>;
87
+ declare function useContent<T = JSONMappable>(fragmentRef: FragmentRef, options: {
88
+ optional: false;
89
+ }): ProxiedContent<T>;
90
+ declare function useContent<T = JSONMappable>(fragmentRef: FragmentRef, options: {
91
+ optional: true;
92
+ }): ProxiedContent<T> | undefined;
93
+ declare function unproxy<T>(proxy: T): Unproxied<T>;
94
+
95
+ declare function useSetFragment(): (fragmentRef: {
96
+ __id: string;
97
+ } | string, updater: (draft: unknown) => void) => void;
50
98
 
51
99
  declare function useSuperglue(): SuperglueState;
52
- declare function useContent<T = JSONMappable>(): T;
53
100
 
54
- declare const prepareStore: (store: SuperglueStore, initialPage: VisitResponse, path: string) => void;
101
+ declare const prepareStore: (store: SuperglueStore, initialPage: SaveResponse, path: string) => void;
55
102
  declare const setup: ({ initialPage, baseUrl, path, store, buildVisitAndRemote, history, navigatorRef, }: SetupProps) => {
56
103
  visit: ApplicationVisit;
57
104
  remote: ApplicationRemote;
58
105
  nextHistory: history.History;
59
106
  initialPageKey: string;
60
107
  ujs: Handlers;
108
+ streamActions: StreamActions;
61
109
  };
62
110
  declare function Application({ initialPage, baseUrl, path, store, buildVisitAndRemote, history, mapping, ...rest }: ApplicationProps): React.JSX.Element;
63
111
 
64
- export { Application, ApplicationProps, ApplicationRemote, ApplicationVisit, FetchArgs, GRAFTING_ERROR, GRAFTING_SUCCESS, Handlers, JSONMappable, JSONValue, Keypath, NavigateTo, NavigationContext, NavigationContextProps, NavigationProvider, NavigationProviderProps, PageKey, SetupProps, SuperglueState, SuperglueStore, VisitResponse, beforeFetch, beforeRemote, beforeVisit, copyPage, getIn, prepareStore, removePage, saveResponse, setup, updateFragments, urlToPageKey, useContent, useSuperglue };
112
+ export { Application, ApplicationProps, ApplicationRemote, ApplicationVisit, FetchArgs, GRAFTING_ERROR, GRAFTING_SUCCESS, Handlers, JSONMappable, JSONValue, Keypath, NavigateTo, NavigationContext, NavigationContextProps, NavigationProvider, NavigationProviderProps, PageKey, PageResponse, SaveResponse, SetupProps, SuperglueState, SuperglueStore, Unproxied, beforeFetch, beforeRemote, beforeVisit, copyPage, getIn, prepareStore, receiveResponse, removePage, saveResponse, setup, unproxy, urlToPageKey, useContent, useSetFragment, useStreamSource, useSuperglue };