-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
InertiaProvider.ts
232 lines (206 loc) · 6.74 KB
/
InertiaProvider.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import { Redirect } from '@adonisjs/http-server/build/src/Redirect';
import { ApplicationContract } from '@ioc:Adonis/Core/Application';
import { ConfigContract } from '@ioc:Adonis/Core/Config';
import { HttpContextConstructorContract } from '@ioc:Adonis/Core/HttpContext';
import { RequestConstructorContract, RequestContract } from '@ioc:Adonis/Core/Request';
import { RedirectContract, ResponseConstructorContract } from '@ioc:Adonis/Core/Response';
import { RouterContract } from '@ioc:Adonis/Core/Route';
import Validator, { ErrorReporterConstructorContract } from '@ioc:Adonis/Core/Validator';
import { ViewContract } from '@ioc:Adonis/Core/View';
import { ResponseProps } from '@ioc:EidelLev/Inertia';
import { encode } from 'html-entities';
import { Inertia } from '../../src/Inertia';
import { inertiaHelper } from '../../src/inertiaHelper';
import { HEADERS } from '../../src/utils';
import InertiaMiddleware from '../../middleware/Inertia';
/*
|--------------------------------------------------------------------------
| Inertia Provider
|--------------------------------------------------------------------------
*/
export default class InertiaProvider {
constructor(protected app: ApplicationContract) {}
public static needsApplication = true;
/**
* Register the `inertia` view global
*/
private registerInertiaViewGlobal(View: ViewContract) {
View.global('inertia', (page: Record<string, unknown> = {}) => {
if (page.ssrBody) {
return page.ssrBody;
}
return `<div id="app" data-page="${encode(JSON.stringify(page))}"></div>`;
});
}
/**
* Register the `inertiaHead` view global
*/
private registerInertiaHeadViewGlobal(View: ViewContract) {
View.global('inertiaHead', (page: Record<string, unknown>) => {
const { ssrHead = [] }: { ssrHead?: string[] } = page || {};
return ssrHead.join();
});
}
private registerInertiaTag(View: ViewContract) {
View.registerTag({
block: false,
tagName: 'inertia',
seekable: false,
compile(_, buffer, token) {
buffer.writeExpression(
`\n
out += template.sharedState.inertia(state.page)
`,
token.filename,
token.loc.start.line,
);
},
});
}
private registerInertiaHeadTag(View: ViewContract) {
View.registerTag({
block: false,
tagName: 'inertiaHead',
seekable: false,
compile(_, buffer, token) {
buffer.writeExpression(
`\n
out += template.sharedState.inertiaHead(state.page)
`,
token.filename,
token.loc.start.line,
);
},
});
}
/*
* Hook inertia into ctx during request cycle
*/
private registerInertia(
Application: ApplicationContract,
HttpContext: HttpContextConstructorContract,
Config: ConfigContract,
) {
const config = Config.get('inertia.inertia', { view: 'app' });
HttpContext.getter(
'inertia',
function inertia() {
return new Inertia(Application, this, config);
},
false,
);
}
/*
* Register `inertia` helper on request object
*/
private registerInertiaHelper(request: RequestConstructorContract) {
request.getter(
'inertia',
function inertia() {
return () => inertiaHelper(this);
},
false,
);
}
/**
* Registers inertia binding
*/
public registerBinding() {
this.app.container.bind('EidelLev/Inertia/Middleware', () => InertiaMiddleware);
this.app.container.singleton('EidelLev/Inertia', () => ({
share: Inertia.share,
version: Inertia.version,
manifestFile: Inertia.manifestFile,
}));
}
/**
* Registers custom validation negotiator
* https://preview.adonisjs.com/releases/core/preview-rc-2#validator
*/
public registerNegotiator({ validator }: typeof Validator) {
validator.negotiator((request: RequestContract): ErrorReporterConstructorContract => {
if (request.inertia()) {
return validator.reporters.vanilla;
}
if (request.ajax()) {
return validator.reporters.api;
}
switch (request.accepts(['html', 'application/vnd.api+json', 'json'])) {
case 'html':
case null:
return validator.reporters.vanilla;
case 'json':
return validator.reporters.api;
case 'application/vnd.api+json':
return validator.reporters.jsonapi;
}
});
}
/**
* Registers the Inertia route helper
*/
public registerRouteHelper(Route: RouterContract): void {
Route.inertia = (pattern: string, component: string, pageOnlyProps: ResponseProps = {}) => {
Route.get(pattern, ({ inertia }) => {
return inertia.render(component, {}, pageOnlyProps);
});
return Route;
};
}
/**
* Set HTTP code 303 after a PUT, PATCH or POST request so the redirect is treated as GET request
* https://inertiajs.com/redirects#303-response-code
*/
public registerRedirect(Response: ResponseConstructorContract) {
Response.macro(
'redirect',
function (path?: string, forwardQueryString: boolean = false, statusCode = 302): RedirectContract | void {
const isInertia = this.request.rawHeaders.includes(HEADERS.INERTIA_HEADER);
const method = this.request.method;
let finalStatusCode = statusCode;
if (isInertia && statusCode === 302 && method && method && ['PUT', 'PATCH', 'DELETE'].includes(method)) {
finalStatusCode = 303;
}
// @ts-ignore
const handler = new Redirect(this.request, this, this.router);
if (forwardQueryString) {
handler.withQs();
}
if (path === 'back') {
return handler.status(finalStatusCode).back();
}
if (path) {
return handler.status(finalStatusCode).toPath(path);
}
handler.status(finalStatusCode);
return handler;
},
);
}
public boot(): void {
this.app.container.withBindings(
[
'Adonis/Core/HttpContext',
'Adonis/Core/View',
'Adonis/Core/Config',
'Adonis/Core/Request',
'Adonis/Core/Response',
'Adonis/Core/Validator',
'Adonis/Core/Route',
'Adonis/Core/Application',
],
(HttpContext, View, Config, Request, Response, Validator, Route, Application) => {
this.registerInertia(Application, HttpContext, Config);
this.registerInertiaViewGlobal(View);
this.registerInertiaHeadViewGlobal(View);
this.registerInertiaTag(View);
this.registerInertiaHeadTag(View);
this.registerInertiaHelper(Request);
this.registerRedirect(Response);
this.registerNegotiator(Validator);
this.registerBinding();
this.registerRouteHelper(Route);
},
);
}
}