From 1c4cb49d5cfb71637209e9a26c9063d98a9fb725 Mon Sep 17 00:00:00 2001 From: Tomster Date: Sat, 6 Jul 2024 14:10:09 +0000 Subject: [PATCH] Chapter 11 EmberData with RequestManager and Builders --- Commit: ember-learn/super-rentals-tutorial@f76672816e0f8fe5d6bd96e4ffa5e8f81fd2a2e4 Script: https://github.com/ember-learn/super-rentals-tutorial/blob/f76672816e0f8fe5d6bd96e4ffa5e8f81fd2a2e4/.github/workflows/build.yml Logs: https://github.com/ember-learn/super-rentals-tutorial/commit/f76672816e0f8fe5d6bd96e4ffa5e8f81fd2a2e4/checks --- app/adapters/application.js | 9 --------- app/app.js | 5 +++++ app/routes/index.js | 4 +++- app/routes/rental.js | 6 +++++- app/serializers/application.js | 3 --- app/services/request-manager.js | 11 +++++++++++ app/services/store.js | 8 +++++++- app/utils/handlers.js | 10 ++++++++++ public/api/rentals.json | 6 +++--- public/api/rentals/downtown-charm.json | 2 +- public/api/rentals/grand-old-mansion.json | 2 +- public/api/rentals/urban-living.json | 2 +- yarn.lock | 24 +++++++++++------------ 13 files changed, 59 insertions(+), 33 deletions(-) delete mode 100644 app/adapters/application.js delete mode 100644 app/serializers/application.js create mode 100644 app/services/request-manager.js create mode 100644 app/utils/handlers.js diff --git a/app/adapters/application.js b/app/adapters/application.js deleted file mode 100644 index ad80ff04a..000000000 --- a/app/adapters/application.js +++ /dev/null @@ -1,9 +0,0 @@ -import JSONAPIAdapter from '@ember-data/adapter/json-api'; - -export default class ApplicationAdapter extends JSONAPIAdapter { - namespace = 'api'; - - buildURL(...args) { - return `${super.buildURL(...args)}.json`; - } -} diff --git a/app/app.js b/app/app.js index 3d528374b..36131b293 100644 --- a/app/app.js +++ b/app/app.js @@ -2,6 +2,11 @@ import Application from '@ember/application'; import Resolver from 'ember-resolver'; import loadInitializers from 'ember-load-initializers'; import config from 'super-rentals/config/environment'; +import { setBuildURLConfig } from '@ember-data/request-utils'; + +setBuildURLConfig({ + namespace: 'api', +}); export default class App extends Application { modulePrefix = config.modulePrefix; diff --git a/app/routes/index.js b/app/routes/index.js index 63814d9f0..920f406fd 100644 --- a/app/routes/index.js +++ b/app/routes/index.js @@ -1,10 +1,12 @@ import Route from '@ember/routing/route'; import { service } from '@ember/service'; +import { query } from '@ember-data/json-api/request'; export default class IndexRoute extends Route { @service store; async model() { - return this.store.findAll('rental'); + const { content } = await this.store.request(query('rental')); + return content.data; } } diff --git a/app/routes/rental.js b/app/routes/rental.js index ad8426892..e8102aa67 100644 --- a/app/routes/rental.js +++ b/app/routes/rental.js @@ -1,10 +1,14 @@ import Route from '@ember/routing/route'; import { service } from '@ember/service'; +import { findRecord } from '@ember-data/json-api/request'; export default class RentalRoute extends Route { @service store; async model(params) { - return this.store.findRecord('rental', params.rental_id); + const { content } = await this.store.request( + findRecord('rental', params.rental_id), + ); + return content.data; } } diff --git a/app/serializers/application.js b/app/serializers/application.js deleted file mode 100644 index 2341d47cf..000000000 --- a/app/serializers/application.js +++ /dev/null @@ -1,3 +0,0 @@ -import JSONAPISerializer from '@ember-data/serializer/json-api'; - -export default class ApplicationSerializer extends JSONAPISerializer {} diff --git a/app/services/request-manager.js b/app/services/request-manager.js new file mode 100644 index 000000000..148424598 --- /dev/null +++ b/app/services/request-manager.js @@ -0,0 +1,11 @@ +import BaseRequestManager from '@ember-data/request'; +import Fetch from '@ember-data/request/fetch'; +import { JsonSuffixHandler } from 'super-rentals/utils/handlers'; + +export default class RequestManager extends BaseRequestManager { + constructor(args) { + super(args); + + this.use([JsonSuffixHandler, Fetch]); + } +} diff --git a/app/services/store.js b/app/services/store.js index f41e1d3d9..5d26b4cfd 100644 --- a/app/services/store.js +++ b/app/services/store.js @@ -1 +1,7 @@ -export { default } from 'ember-data/store'; +// eslint-disable-next-line ember/use-ember-data-rfc-395-imports +import BaseStore from 'ember-data/store'; +import { service } from '@ember/service'; + +export default class Store extends BaseStore { + @service requestManager; +} diff --git a/app/utils/handlers.js b/app/utils/handlers.js new file mode 100644 index 000000000..7101c96a1 --- /dev/null +++ b/app/utils/handlers.js @@ -0,0 +1,10 @@ +export const JsonSuffixHandler = { + request(context, next) { + const { request } = context; + const updatedRequest = Object.assign({}, request, { + url: request.url + '.json', + }); + + return next(updatedRequest); + }, +}; diff --git a/public/api/rentals.json b/public/api/rentals.json index d9ce0f561..b20ffcda3 100644 --- a/public/api/rentals.json +++ b/public/api/rentals.json @@ -1,7 +1,7 @@ { "data": [ { - "type": "rentals", + "type": "rental", "id": "grand-old-mansion", "attributes": { "title": "Grand Old Mansion", @@ -18,7 +18,7 @@ } }, { - "type": "rentals", + "type": "rental", "id": "urban-living", "attributes": { "title": "Urban Living", @@ -35,7 +35,7 @@ } }, { - "type": "rentals", + "type": "rental", "id": "downtown-charm", "attributes": { "title": "Downtown Charm", diff --git a/public/api/rentals/downtown-charm.json b/public/api/rentals/downtown-charm.json index 29b1f53f8..ed9d34dfb 100644 --- a/public/api/rentals/downtown-charm.json +++ b/public/api/rentals/downtown-charm.json @@ -1,6 +1,6 @@ { "data": { - "type": "rentals", + "type": "rental", "id": "downtown-charm", "attributes": { "title": "Downtown Charm", diff --git a/public/api/rentals/grand-old-mansion.json b/public/api/rentals/grand-old-mansion.json index 00fdd56d9..76732b15d 100644 --- a/public/api/rentals/grand-old-mansion.json +++ b/public/api/rentals/grand-old-mansion.json @@ -1,6 +1,6 @@ { "data": { - "type": "rentals", + "type": "rental", "id": "grand-old-mansion", "attributes": { "title": "Grand Old Mansion", diff --git a/public/api/rentals/urban-living.json b/public/api/rentals/urban-living.json index 26d9dab59..51c1c0085 100644 --- a/public/api/rentals/urban-living.json +++ b/public/api/rentals/urban-living.json @@ -1,6 +1,6 @@ { "data": { - "type": "rentals", + "type": "rental", "id": "urban-living", "attributes": { "title": "Urban Living", diff --git a/yarn.lock b/yarn.lock index 00693363f..689d59fdc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1079,9 +1079,9 @@ minimist "^1.2.0" "@csstools/css-parser-algorithms@^2.3.1": - version "2.7.0" - resolved "https://registry.yarnpkg.com/@csstools/css-parser-algorithms/-/css-parser-algorithms-2.7.0.tgz#ee41f252438b97045db2528c1a999e95f15610d8" - integrity sha512-qvBMcOU/uWFCH/VO0MYe0AMs0BGMWAt6FTryMbFIKYtZtVnqTZtT8ktv5o718llkaGZWomJezJZjq3vJDHeJNQ== + version "2.7.1" + resolved "https://registry.yarnpkg.com/@csstools/css-parser-algorithms/-/css-parser-algorithms-2.7.1.tgz#6d93a8f7d8aeb7cd9ed0868f946e46f021b6aa70" + integrity sha512-2SJS42gxmACHgikc1WGesXLIT8d/q2l0UFM7TaEeIzdFCE/FPMtTiizcPGGJtlPo2xuQzY09OhrLTzRxqJqwGw== "@csstools/css-tokenizer@^2.2.0": version "2.4.1" @@ -1089,9 +1089,9 @@ integrity sha512-eQ9DIktFJBhGjioABJRtUucoWR2mwllurfnM8LuNGAqX3ViZXaUchqk+1s7jjtkFiT9ySdACsFEA3etErkALUg== "@csstools/media-query-list-parser@^2.1.4": - version "2.1.12" - resolved "https://registry.yarnpkg.com/@csstools/media-query-list-parser/-/media-query-list-parser-2.1.12.tgz#ec7485667efbaad420b3414dc1dc19dc7a49f626" - integrity sha512-t1/CdyVJzOQUiGUcIBXRzTAkWTFPxiPnoKwowKW2z9Uj78c2bBWI/X94BeVfUwVq1xtCjD7dnO8kS6WONgp8Jw== + version "2.1.13" + resolved "https://registry.yarnpkg.com/@csstools/media-query-list-parser/-/media-query-list-parser-2.1.13.tgz#f00be93f6bede07c14ddf51a168ad2748e4fe9e5" + integrity sha512-XaHr+16KRU9Gf8XLi3q8kDlI18d5vzKSKCY510Vrtc9iNR0NJzbY9hhTmwhzYZj/ZwGL4VmB3TA9hJW0Um2qFA== "@csstools/selector-specificity@^3.0.0": version "3.1.1" @@ -4105,9 +4105,9 @@ ee-first@1.1.1: integrity sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow== electron-to-chromium@^1.4.796: - version "1.4.817" - resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.817.tgz#da9808e35212408cf4052b51679786fcb54fec39" - integrity sha512-3znu+lZMIbTe8ZOs360OMJvVroVF2NpNI8T5jfLnDetVvj0uNmIucZzQVYMSJfsu9f47Ssox1Gt46PR+R+1JUg== + version "1.4.818" + resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.818.tgz#7762c8bfd15a07c3833b7f5deed990e9e5a4c24f" + integrity sha512-eGvIk2V0dGImV9gWLq8fDfTTsCAeMDwZqEPMr+jMInxZdnp9Us8UpovYpRCf9NQ7VOFgrN2doNSgvISbsbNpxA== ember-auto-import@^2.6.0, ember-auto-import@^2.6.3, ember-auto-import@^2.7.3: version "2.7.4" @@ -7306,9 +7306,9 @@ lower-case@^2.0.2: tslib "^2.0.3" lru-cache@^10.2.0: - version "10.3.0" - resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-10.3.0.tgz#4a4aaf10c84658ab70f79a85a9a3f1e1fb11196b" - integrity sha512-CQl19J/g+Hbjbv4Y3mFNNXFEL/5t/KCg8POCuUqd4rMKjGG+j1ybER83hxV58zL+dFI1PTkt3GNFSHRt+d8qEQ== + version "10.3.1" + resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-10.3.1.tgz#a37050586f84ccfdb570148a253bf1632a29ef44" + integrity sha512-9/8QXrtbGeMB6LxwQd4x1tIMnsmUxMvIH/qWGsccz6bt9Uln3S+sgAaqfQNhbGA8ufzs2fHuP/yqapGgP9Hh2g== lru-cache@^5.1.1: version "5.1.1"