|
1 | | -/* global FastBoot */ |
2 | | -import { deprecate } from '@ember/application/deprecations'; |
3 | | -import { computed, get } from '@ember/object'; |
4 | | -import { deprecatingAlias, readOnly } from '@ember/object/computed'; |
5 | | -import { assert } from '@ember/debug'; |
6 | | -import EObject from '@ember/object'; |
7 | | -import Service from '@ember/service'; |
8 | | - |
9 | | -const RequestObject = EObject.extend({ |
10 | | - init() { |
11 | | - this._super(...arguments); |
12 | | - |
13 | | - let request = this.request; |
14 | | - delete this.request; |
15 | | - |
16 | | - this.method = request.method; |
17 | | - this.body = request.body; |
18 | | - this.cookies = request.cookies; |
19 | | - this.headers = request.headers; |
20 | | - this.queryParams = request.queryParams; |
21 | | - this.path = request.path; |
22 | | - this.protocol = request.protocol; |
23 | | - this._host = function() { |
24 | | - return request.host(); |
25 | | - }; |
26 | | - }, |
27 | | - |
28 | | - host: computed(function() { |
29 | | - return this._host(); |
30 | | - }) |
31 | | -}); |
32 | | - |
33 | | -const Shoebox = EObject.extend({ |
34 | | - put(key, value) { |
35 | | - assert('shoebox.put is only invoked from the FastBoot rendered application', this.get('fastboot.isFastBoot')); |
36 | | - assert('the provided key is a string', typeof key === 'string'); |
37 | | - |
38 | | - let fastbootInfo = this.get('fastboot._fastbootInfo'); |
39 | | - if (!fastbootInfo.shoebox) { fastbootInfo.shoebox = {}; } |
40 | | - |
41 | | - fastbootInfo.shoebox[key] = value; |
42 | | - }, |
43 | | - |
44 | | - retrieve(key) { |
45 | | - if (this.get('fastboot.isFastBoot')) { |
46 | | - let shoebox = this.get('fastboot._fastbootInfo.shoebox'); |
47 | | - if (!shoebox) { return; } |
48 | | - |
49 | | - return shoebox[key]; |
50 | | - } |
51 | | - |
52 | | - let shoeboxItem = this.get(key); |
53 | | - if (shoeboxItem) { return shoeboxItem; } |
54 | | - |
55 | | - let el = document.querySelector(`#shoebox-${key}`); |
56 | | - if (!el) { return; } |
57 | | - let valueString = el.textContent; |
58 | | - if (!valueString) { return; } |
59 | | - |
60 | | - shoeboxItem = JSON.parse(valueString); |
61 | | - this.set(key, shoeboxItem); |
62 | | - |
63 | | - return shoeboxItem; |
64 | | - } |
65 | | -}); |
66 | | - |
67 | | -const FastBootService = Service.extend({ |
68 | | - cookies: deprecatingAlias('request.cookies', { id: 'fastboot.cookies-to-request', until: '0.9.9' }), |
69 | | - headers: deprecatingAlias('request.headers', { id: 'fastboot.headers-to-request', until: '0.9.9' }), |
70 | | - isFastBoot: typeof FastBoot !== 'undefined', |
71 | | - |
72 | | - init() { |
73 | | - this._super(...arguments); |
74 | | - |
75 | | - let shoebox = Shoebox.create({ fastboot: this }); |
76 | | - this.set('shoebox', shoebox); |
77 | | - }, |
78 | | - |
79 | | - host: computed(function() { |
80 | | - deprecate( |
81 | | - 'Usage of fastboot service\'s `host` property is deprecated. Please use `request.host` instead.', |
82 | | - false, |
83 | | - { id: 'fastboot.host-to-request', until: '0.9.9' } |
84 | | - ); |
85 | | - |
86 | | - return this._fastbootInfo.request.host(); |
87 | | - }), |
88 | | - |
89 | | - response: readOnly('_fastbootInfo.response'), |
90 | | - metadata: readOnly('_fastbootInfo.metadata'), |
91 | | - |
92 | | - request: computed(function() { |
93 | | - if (!this.isFastBoot) return null; |
94 | | - return RequestObject.create({ request: get(this, '_fastbootInfo.request') }); |
95 | | - }), |
96 | | - |
97 | | - deferRendering(promise) { |
98 | | - assert('deferRendering requires a promise or thennable object', typeof promise.then === 'function'); |
99 | | - this._fastbootInfo.deferRendering(promise); |
100 | | - } |
101 | | -}); |
102 | | - |
103 | | -export default FastBootService; |
| 1 | +export { default } from 'ember-cli-fastboot/services/fastboot'; |
0 commit comments