Skip to content

Commit

Permalink
Better example for making own builders
Browse files Browse the repository at this point in the history
  • Loading branch information
Baltazore committed Apr 4, 2024
1 parent 08302a8 commit cfc08b2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 48 deletions.
70 changes: 25 additions & 45 deletions snippets/adapters/path-for-type/utils.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,40 @@
// utils/my-backend-request-builders.js
import { camelize } from '@ember/string';
import { pluralize } from 'ember-inflector';
import { recordIdentifierFor } from '@ember-data/store';

import {
findRecord as restFindRecord,
query as restQuery,
createRecord as restCreateRecord,
updateRecord as restUpdateRecord,
deleteRecord as restDeleteRecord,
} from '@ember-data/rest/request';
import { buildBaseURL, buildQueryParams } from '@ember-data/request-utils';

const _customResourcePath = (identifierType) => {
return `collections/${camelize(pluralize(identifierType))}/records`;
};

const findRecord = (identifierType, id, options) => {
options = {
...options,
resourcePath: _customResourcePath(identifierType),
};
return restFindRecord(identifierType, id, options);
};

const query = (identifierType, query, options) => {
options = {
...options,
resourcePath: _customResourcePath(identifierType),
};
return restQuery(identifierType, query, options);
};
async function findRecord(typeOrIdentifier, idOrOptions, maybeOptions) {
const identifier = typeof typeOrIdentifier === 'string' ? { type: typeOrIdentifier, id } : typeOrIdentifier;
const options = ((typeof typeOrIdentifier === 'string' ? maybeOptions : idOrOptions) || {});

const createRecord = (record, options) => {
const identifier = recordIdentifierFor(record);
options = {
...options,
const urlOptions = {
op: 'findRecord',
identifier,
resourcePath: _customResourcePath(identifier.type),
};
return restCreateRecord(record, options);
};

const updateRecord = (record, options) => {
const identifier = recordIdentifierFor(record);
options = {
...options,
resourcePath: _customResourcePath(identifier.type),
const url = buildBaseURL(urlOptions);
const headers = new Headers();
headers.append('Accept', 'application/vnd.api+json');
headers.append('Content-Type', 'application/vnd.api+json');

return {
url: options.include?.length
? `${url}?${buildQueryParams({ include: options.include }, options.urlParamsSettings)}`
: url,
method: 'GET',
headers,
op: 'findRecord',
records: [identifier],
};
return restUpdateRecord(record, options);
};

const deleteRecord = (record, options) => {
const identifier = recordIdentifierFor(record);
options = {
...options,
resourcePath: _customResourcePath(identifier.type),
};
return restDeleteRecord(record, options);
}

export default {
findRecord
};

export { findRecord, query, createRecord, updateRecord, deleteRecord };
4 changes: 1 addition & 3 deletions snippets/fetching-data/find-all/new.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { query } from '@ember-data/json-api/request';

const result = await store.request(query('user'));
const users = result.content.data;
// or
await store.request(query('user'));
const users = store.peekAll('user')

0 comments on commit cfc08b2

Please sign in to comment.