diff --git a/addon/serializers/github-blob.js b/addon/serializers/github-blob.js index eb80607..e02345d 100644 --- a/addon/serializers/github-blob.js +++ b/addon/serializers/github-blob.js @@ -1,16 +1,3 @@ -import GithubSerializer from './github'; +import GithubSerializer from 'ember-data-github/serializers/github'; -export default GithubSerializer.extend({ - normalize(modelClass, resourceHash, prop) { - let normalizedHash = { - id: resourceHash.sha, - sha: resourceHash.sha, - url: resourceHash.url, - content: resourceHash.content, - encoding: resourceHash.encoding, - size: resourceHash.size - }; - - return this._super(modelClass, normalizedHash, prop); - } -}); +export default GithubSerializer.extend({}); diff --git a/addon/serializers/github-branch.js b/addon/serializers/github-branch.js index 983bada..06a0792 100644 --- a/addon/serializers/github-branch.js +++ b/addon/serializers/github-branch.js @@ -1,13 +1,9 @@ -import GithubSerializer from './github'; +import GithubSerializer from 'ember-data-github/serializers/github'; export default GithubSerializer.extend({ - normalize(type, hash, prop) { - hash = { - id: hash.recordId || hash.commit.url.replace('https://api.github.com/repos/', '').replace(/\/commits\/.+/, `/branches/${hash.name}`), - name: hash.name, - commit: hash.commit, - protected: hash.protected - }; - return this._super(type, hash, prop); + normalize(modelClass, resourceHash, prop) { + resourceHash.id = resourceHash.recordId || resourceHash.commit.url.replace('https://api.github.com/repos/', '').replace(/\/commits\/.+/, `/branches/${resourceHash.name}`); + + return this._super(modelClass, resourceHash, prop); } }); diff --git a/addon/serializers/github-organization.js b/addon/serializers/github-organization.js index acc9906..97d1f22 100644 --- a/addon/serializers/github-organization.js +++ b/addon/serializers/github-organization.js @@ -1,17 +1,12 @@ -import GithubSerializer from './github'; +import GithubSerializer from 'ember-data-github/serializers/github'; export default GithubSerializer.extend({ - normalize(type, hash, prop) { - hash = { - id: hash.recordId || hash.login, - login: hash.login, - name: hash.name, - avatarUrl: hash.avatar_url, - links: { - users: hash.members_url.replace(/\{\/member\}/, ''), - repositories: hash.repos_url - } + normalize(modelClass, resourceHash, prop) { + resourceHash.id = resourceHash.recordId || resourceHash.login; + resourceHash.links = { + users: resourceHash.members_url.replace(/\{\/member\}/, ''), + repositories: resourceHash.repos_url }; - return this._super(type, hash, prop); + return this._super(modelClass, resourceHash, prop); } }); diff --git a/addon/serializers/github-pull.js b/addon/serializers/github-pull.js index db401b8..eb92257 100644 --- a/addon/serializers/github-pull.js +++ b/addon/serializers/github-pull.js @@ -1,24 +1,12 @@ -import GithubSerializer from './github'; +import GithubSerializer from 'ember-data-github/serializers/github'; export default GithubSerializer.extend({ normalize(modelClass, resourceHash, prop) { - let hash = { - id: resourceHash.id, - number: resourceHash.number, - title: resourceHash.title, - state: resourceHash.state, - htmlUrl: resourceHash.html_url, - body: resourceHash.body, - createdAt: resourceHash.created_at, - updatedAt: resourceHash.updated_at, - closedAt: resourceHash.closed_at, - mergedAt: resourceHash.merged_at, - userAvatarUrl: resourceHash.user.avatar_url, - userLogin: resourceHash.user.login, - links: { - user: resourceHash.user.url - } + resourceHash.user_avatar_url = resourceHash.user.avatar_url; + resourceHash.user_login = resourceHash.user.login, + resourceHash.links = { + user: resourceHash.user.url }; - return this._super(modelClass, hash, prop); + return this._super(modelClass, resourceHash, prop); } }); diff --git a/addon/serializers/github-release.js b/addon/serializers/github-release.js index 50cf63e..431adfd 100644 --- a/addon/serializers/github-release.js +++ b/addon/serializers/github-release.js @@ -1,11 +1,6 @@ -import GithubSerializer from './github'; -import { decamelize } from '@ember/string'; +import GithubSerializer from 'ember-data-github/serializers/github'; export default GithubSerializer.extend({ - keyForAttribute(attr) { - return decamelize(attr); - }, - normalize(modelClass, resourceHash, prop) { resourceHash.links = { user: resourceHash.author.url diff --git a/addon/serializers/github-repository.js b/addon/serializers/github-repository.js index ebdb827..aa33bf3 100644 --- a/addon/serializers/github-repository.js +++ b/addon/serializers/github-repository.js @@ -1,27 +1,15 @@ -import GithubSerializer from './github'; +import GithubSerializer from 'ember-data-github/serializers/github'; export default GithubSerializer.extend({ normalize(modelClass, resourceHash, prop) { - let hash = { - id: resourceHash.recordId || resourceHash.full_name, - fullName: resourceHash.full_name, - name: resourceHash.name, - description: resourceHash.description, - htmlUrl: resourceHash.html_url, - language: resourceHash.language, - fork: resourceHash.fork, - private: resourceHash.private, - createdAt: resourceHash.created_at, - updatedAt: resourceHash.updated_at, - pushedAt: resourceHash.pushed_at, - links: { - owner: resourceHash.owner.url, - defaultBranch: `${resourceHash.url}/branches/${resourceHash.default_branch}`, - branches: `${resourceHash.url}/branches`, - pulls: `${resourceHash.url}/pulls`, - releases: `${resourceHash.url}/releases` - } + resourceHash.id = resourceHash.recordId || resourceHash.full_name, + resourceHash.links = { + owner: resourceHash.owner.url, + defaultBranch: `${resourceHash.url}/branches/${resourceHash.default_branch}`, + branches: `${resourceHash.url}/branches`, + pulls: `${resourceHash.url}/pulls`, + releases: `${resourceHash.url}/releases` }; - return this._super(modelClass, hash, prop); + return this._super(modelClass, resourceHash, prop); } }); diff --git a/addon/serializers/github-tree.js b/addon/serializers/github-tree.js index 395312d..a39d16b 100644 --- a/addon/serializers/github-tree.js +++ b/addon/serializers/github-tree.js @@ -1,4 +1,4 @@ -import GithubSerializer from './github'; +import GithubSerializer from 'ember-data-github/serializers/github'; export default GithubSerializer.extend({ normalize(modelClass, resourceHash, prop) { @@ -7,24 +7,20 @@ export default GithubSerializer.extend({ let treeItems = resourceHash.tree .filter(item => item.type === 'tree'); - let normalizedHash = { - id: resourceHash.sha, - sha: resourceHash.sha, - url: resourceHash.url, - files: blobItems.reduce((files, blob) => { - files[blob.path] = blob.sha; - return files; - }, {}), - directories: treeItems.reduce((files, tree) => { - files[tree.path] = tree.sha; - return files; - }, {}), - truncated: resourceHash.truncated, - links: { - blobs: blobItems.map(blob => blob.url), - trees: treeItems.map(tree => tree.url) - } - }; - return this._super(modelClass, normalizedHash, prop); + resourceHash.id = resourceHash.sha; + resourceHash.files = blobItems.reduce((files, blob) => { + files[blob.path] = blob.sha; + return files; + }, {}); + resourceHash.directories = treeItems.reduce((files, tree) => { + files[tree.path] = tree.sha; + return files; + }, {}); + resourceHash.links = { + blobs: blobItems.map(blob => blob.url), + trees: treeItems.map(tree => tree.url) + } + + return this._super(modelClass, resourceHash, prop); } }); diff --git a/addon/serializers/github-user.js b/addon/serializers/github-user.js index dbd17bd..25438c0 100644 --- a/addon/serializers/github-user.js +++ b/addon/serializers/github-user.js @@ -1,4 +1,4 @@ -import GithubSerializer from './github'; +import GithubSerializer from 'ember-data-github/serializers/github'; export default GithubSerializer.extend({ normalizeResponse(store, primaryModelClass, payload, id, requestType) { @@ -9,29 +9,11 @@ export default GithubSerializer.extend({ }, normalize(modelClass, resourceHash, prop) { - let normalizedHash = { - id: resourceHash.recordId || resourceHash.login, - login: resourceHash.login, - name: resourceHash.name, - type: resourceHash.type, - avatarUrl: resourceHash.avatar_url, - htmlUrl: resourceHash.html_url, - publicRepos: resourceHash.public_repos, - publicGists: resourceHash.public_gists, - followers: resourceHash.followers, - following: resourceHash.following, - createdAt: resourceHash.created_at, - updatedAt: resourceHash.updated_at, - url: resourceHash.url, - company: resourceHash.company, - blog: resourceHash.blog, - location: resourceHash.location, - email: resourceHash.email, - bio: resourceHash.bio, - links: { - repositories: resourceHash.repos_url - } - }; - return this._super(modelClass, normalizedHash, prop); + resourceHash.id = resourceHash.recordId || resourceHash.login; + resourceHash.links = { + repositories: resourceHash.repos_url + } + + return this._super(modelClass, resourceHash, prop); } }); diff --git a/addon/serializers/github.js b/addon/serializers/github.js index 433b8df..10c527d 100644 --- a/addon/serializers/github.js +++ b/addon/serializers/github.js @@ -3,8 +3,12 @@ import { get } from '@ember/object'; import { isNone } from '@ember/utils'; import { pluralize } from 'ember-inflector'; import DS from 'ember-data'; +import { decamelize } from '@ember/string'; export default DS.RESTSerializer.extend({ + keyForAttribute(attr) { + return decamelize(attr); + }, normalizeResponse(store, primaryModelClass, payload, id, requestType) { payload.recordId = id; diff --git a/tests/dummy/mirage/factories/github-user.js b/tests/dummy/mirage/factories/github-user.js index 8abe588..30192f3 100644 --- a/tests/dummy/mirage/factories/github-user.js +++ b/tests/dummy/mirage/factories/github-user.js @@ -7,7 +7,8 @@ export default Factory.extend({ name: function(i) { return `User ${i}`; }, - type: 'User', + // needs to be the actual related model name for Ember Data reasons? 🤔 + type: 'github-user', avatar_url: function(i) { return `user${i}-avatar.gif`; },