-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from enspandi/auto-discovery
Support auto discovery for mirage and ember-data entities via import.meta.glob
- Loading branch information
Showing
10 changed files
with
220 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { _utilsInflectorCamelize as camelize } from 'miragejs'; | ||
|
||
async function getDefaultExports(modules) { | ||
return Promise.all( | ||
Object.entries(modules).map(([path, importFn]) => | ||
importFn().then((module) => ({ path, defaultExport: module.default })), | ||
), | ||
); | ||
} | ||
|
||
export function entityName(path) { | ||
const filenameWithoutExt = path.split('/').pop().split('.')[0]; | ||
return camelize(filenameWithoutExt); | ||
} | ||
|
||
export async function createConfig(mirageImportMap = {}) { | ||
return { | ||
factories: await importEntities(mirageImportMap.factories), | ||
fixtures: await importEntities(mirageImportMap.fixtures), | ||
models: await importEntities(mirageImportMap.models), | ||
scenarios: await importEntities(mirageImportMap.scenarios), | ||
serializers: await importEntities(mirageImportMap.serializers), | ||
identityManagers: await importEntities(mirageImportMap.identityManagers), | ||
}; | ||
} | ||
|
||
async function importEntities(importMap = {}) { | ||
const modules = await getDefaultExports(importMap); | ||
|
||
return modules.reduce((acc, { path, defaultExport }) => { | ||
const configName = entityName(path); | ||
acc[configName] = defaultExport; | ||
return acc; | ||
}, {}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { Model, belongsTo, hasMany } from 'miragejs'; | ||
import EmberDataSerializer from './ember-data-serializer'; | ||
import { entityName } from './create-config'; | ||
|
||
const MirageModelCache = {}; | ||
const MirageSerializerCache = {}; | ||
|
||
export function importEmberDataModels(store, importMap = {}) { | ||
return Object.keys(importMap).reduce((acc, path) => { | ||
const configName = entityName(path); | ||
acc[configName] = createMirageModel(store, configName); | ||
return acc; | ||
}, {}); | ||
} | ||
|
||
// TODO: Replace with original `applyEmberDataSerializers` function | ||
export function importEmberDataSerializers( | ||
mirageSerializers, | ||
emberDataConfig = {}, | ||
) { | ||
const { store, serializers = {} } = emberDataConfig; | ||
return Object.keys(serializers).reduce((acc, path) => { | ||
const configName = entityName(path); | ||
acc[configName] = createMirageSerializer( | ||
store, | ||
mirageSerializers, | ||
configName, | ||
); | ||
return acc; | ||
}, {}); | ||
} | ||
|
||
function createMirageModel(store, modelName) { | ||
if (MirageModelCache[modelName]) { | ||
return MirageModelCache[modelName]; | ||
} | ||
|
||
const model = store.modelFor(modelName); | ||
const relationships = {}; | ||
|
||
model.eachRelationship((name, r) => { | ||
if (r.kind === 'belongsTo') { | ||
relationships[name] = belongsTo(r.type, r.options); | ||
} else if (r.kind === 'hasMany') { | ||
relationships[name] = hasMany(r.type, r.options); | ||
} | ||
}); | ||
const mirageModel = Model.extend(relationships); | ||
|
||
MirageModelCache[modelName] = mirageModel; | ||
|
||
return mirageModel; | ||
} | ||
|
||
export function createMirageSerializer(store, serializers, serializerName) { | ||
if (MirageSerializerCache[serializerName]) { | ||
return MirageSerializerCache[serializerName]; | ||
} | ||
|
||
let mirageSerializer = | ||
serializers[serializerName] || | ||
serializers.application || | ||
EmberDataSerializer; | ||
|
||
let dsSerializer = store.serializerFor(serializerName); | ||
|
||
let transforms; | ||
let primaryKey = dsSerializer.primaryKey; | ||
let attrs = dsSerializer.attrs; | ||
if (primaryKey || attrs) { | ||
if (attrs) { | ||
let serializer = mirageSerializer.create | ||
? mirageSerializer.create() | ||
: new mirageSerializer(); | ||
|
||
transforms = serializer.transforms || {}; | ||
|
||
Object.keys(attrs).forEach((key) => { | ||
let transform = attrs[key]; | ||
let serializerTransform = serializer.transforms | ||
? serializer.transforms[key] | ||
: {}; | ||
let resolvedTransform = | ||
typeof attrs[key] === 'string' | ||
? { | ||
key: attrs[key], | ||
} | ||
: { | ||
key: attrs[key].key, | ||
}; | ||
|
||
if (transform.serialize !== undefined) { | ||
resolvedTransform.deserialize = transform.serialize; | ||
} | ||
|
||
if (transform.deserialize !== undefined) { | ||
resolvedTransform.serialize = transform.deserialize; | ||
} | ||
|
||
transforms[key] = Object.assign(resolvedTransform, serializerTransform); | ||
}); | ||
} | ||
|
||
mirageSerializer = mirageSerializer.extend({ | ||
primaryKey, | ||
transforms, | ||
}); | ||
} | ||
|
||
MirageSerializerCache[serializerName] = mirageSerializer; | ||
|
||
return mirageSerializer; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export { default as startMirage } from './start-mirage'; | ||
export { createConfig, entityName } from './create-config.js'; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//import { createConfig } from 'ember-mirage'; | ||
//import defaultRoutes from '../routes/default'; | ||
// | ||
//export async function config(store) { | ||
// const { importEmberDataModels, importEmberDataSerializers } = await import( | ||
// 'ember-mirage/ember-data' | ||
// ); | ||
// | ||
// const mirageConfig = await createConfig({ | ||
// factories: import.meta.glob('./factories/*'), | ||
// fixtures: import.meta.glob('./fixtures/*'), | ||
// // Don't import our mirage things that will be auto-discovered | ||
// // models: import.meta.glob('./models/*'), | ||
// // serializers: import.meta.glob('./serializers/*'), | ||
// identityManagers: import.meta.glob('./identity-managers/*'), | ||
// }); | ||
// | ||
// return { | ||
// ...mirageConfig, | ||
// models: { | ||
// // use ember-data model auto discovery | ||
// ...importEmberDataModels(store, import.meta.glob('../models/*')), | ||
// ...mirageConfig.models, | ||
// }, | ||
// // apply ember-data serializer config details to mirage serializers | ||
// serializers: importEmberDataSerializers(store, mirageConfig.serializers), | ||
// routes() { | ||
// this.config({ routes: defaultRoutes }); | ||
// }, | ||
// }; | ||
//} | ||
// |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import mirageConfig from 'test-app/mirage/servers/default'; | ||
import mirageConfig from 'test-app/mirage/servers/manual'; | ||
|
||
import { setupMirage as _setupMirage } from 'ember-mirage/test-support'; | ||
|
||
export function setupMirage(hooks, options) { | ||
options = options || {}; | ||
options.makeServer = options.makeServer || mirageConfig; | ||
import { createServer as _createServer } from 'miragejs'; | ||
|
||
return _setupMirage(hooks, options); | ||
export function setupMirage(hooks, { createServer, config } = {}) { | ||
createServer ??= _createServer; | ||
config ??= mirageConfig; | ||
return _setupMirage(hooks, { createServer, config }); | ||
} |