demo.env
Przemek Suchodolski @przemuh
π°π· corp, π΅π± software-house, πΊπΈ startup
Musician amateur πΈ π₯ πΉ (soundcloud.com/przemuh)
Build complete frontend features,
even if your API doesn't exist.
Sam Selikoff
Ryan Toronto
Β
How to ...π€
Data Management
import React from "react"
import ReactDOM from "react-dom"
import App from "./App"
import { createServer } from "miragejs"
createServer({
routes() {
this.namespace = "api"
this.get("/todos", ({ db }) => {
return db.todos
})
},
})
ReactDOM.render(<App />, document.getElementById("root"))
import { createServer, Model } from "miragejs"
createServer({
models: {
blogPost: Model,
},
})
import { createServer, Model, belongsTo } from "miragejs"
createServer({
models: {
blogPost: Model.extend({
author: belongsTo(),
}),
author: Model,
},
})
import { createServer, Model, belongsTo, hasMany } from "miragejs"
createServer({
models: {
blogPost: Model.extend({
author: belongsTo(),
comments: hasMany(),
}),
author: Model,
comment: Model
},
})
import { createServer, Model, belongsTo, hasMany } from "miragejs"
createServer({
models: {
blogPost: Model.extend({
author: belongsTo(),
comments: hasMany(),
}),
author: Model,
comment: Model
},
routes() {
this.get("/posts", (schema) => schema.blogPosts.all());
}
})
import { createServer, Model, belongsTo, hasMany } from "miragejs"
createServer({
models: {
blogPost: Model.extend({
author: belongsTo(),
comments: hasMany(),
}),
author: Model,
comment: Model
},
routes() {
// [postModel, postModel]
// { attrs, fks, author, comments }
this.get("/posts", (schema) => schema.blogPosts.all());
}
})
import { createServer, Model, belongsTo, hasMany } from "miragejs"
createServer({
models: {
blogPost: Model.extend({
author: belongsTo(),
comments: hasMany(),
}),
author: Model,
comment: Model
},
routes() {
this.get("/posts", (schema) => schema.blogPosts.all());
this.get("/posts/:id", (schema, request) => schema.blogPosts.find(request.params.id));
}
})
import { createServer, Model, belongsTo, hasMany } from "miragejs"
createServer({
models: {
blogPost: Model.extend({
author: belongsTo(),
comments: hasMany(),
}),
author: Model,
comment: Model
},
routes() {
this.get("/posts", (schema) => schema.blogPosts.all());
this.get("/posts/:id", (schema, request) => schema.blogPosts.find(request.params.id));
this.post("/posts", (schema, request) =>
schema.blogPosts.create(JSON.parse(request.requestBody))
);
}
})
import { createServer, Model, belongsTo, hasMany } from "miragejs"
createServer({
models: {
blogPost: Model.extend({
author: belongsTo(),
comments: hasMany(),
}),
author: Model,
comment: Model
},
routes() {
this.get("/posts", (schema) => schema.blogPosts.all());
this.get("/posts/:id", (schema, request) => schema.blogPosts.find(request.params.id));
this.post("/posts", (schema, request) =>
schema.blogPosts.create(JSON.parse(request.requestBody))
);
this.delete("/posts/:id", (schema, request) =>
schema.blogPosts.find(request.params.is).destroy()
);
// ... PUT, PATCH
}
})
import { createServer, Model, belongsTo, hasMany } from "miragejs"
createServer({
models: {
blogPost: Model.extend({
author: belongsTo(),
comments: hasMany(),
}),
author: Model,
comment: Model
},
routes() {
this.resource("blogPosts", { path: "/posts" });
}
})
but not that restful...
this.get("/posts", (schema) => schema.posts.all()) // returns collection
this.get("/posts", (schema) => schema.db.posts.all()) // returns RAW data
export default class ProtectModel extends Model {
buildAttrs() {
return new AttrsBuilder(this.attrs, this.fks);
}
}
class AttrsBuilder {
constructor(attrs, fks) {
this.value = clone(attrs);
this.fks = fks;
}
omit(keys) {}
omitFks() {}
pick(keys) {}
mapId(name) {}
addProperties(obj) {}
mapPropertyName(targetKey, newKey) { }
build() {
return this.value;
}
}
this.get("/folders/:sourceId/:parentId", (schema, { params: { parentId, sourceId } }) => {
const folders = schema.folders.where({
parentId: parentId,
sourceId,
});
return folders.models.map((folder) =>
folder.buildAttrs().pick(["id", "parentId", "path"]).build(),
);
});
this.get("/folders/:sourceId/:parentId", ({ db }, { params: { parentId, sourceId } }) => {
const folders = db.folders.where({
parentId: parentId,
sourceId,
});
return folders.map(({ id, parentId, path }) => ({ id, parentId, path }));
});
Β
createServer({
models: {
folder: Model.extends({
hasMany: files
}),
file: Model.extends({
hasMany: versions
}),
version: Model.extends({
hasMany: matches
}),
match: Model
}
})
class FolderModel extends OurModel {
isSensitve() {
// check if any version of files under this folder
// has some sensitive matches
// π£
}
}
// route
this.get("/sensitive-folders", (schema) =>
schema.folders.all().models.filter(folder => folder.isSensitive())
)
main thread
http request
worker
intercept only
data
mock
Β
Where & When ?
AKA - with mirage.js...