简写
API 变得更加标准化,因此 Mirage 具有 _简写_ 的概念,使您可以轻松编写传统的端点。简写可以取代许多自定义路由处理器,从而极大地简化您的服务器定义。
例如,此函数路由处理器
this.get("/movies", (schema, request) => {
return schema.movies.all()
})
非常标准:它使用与名称相同的集合响应 URL 路径。
此简写形式为
this.get("/movies")
这是一个完整的路由处理器。它从 URL 的最后一部分推断模型名称,并返回相应的集合。
按 ID 返回单个电影同样容易
this.get("/movies/:id")
还有一些简写用于创建和编辑数据。例如,此函数路由处理器创建一部新电影
this.post("/movies", (schema, request) => {
let attrs = JSON.parse(request.requestBody).movie
return schema.movies.create(attrs)
})
它也非常标准:它使用请求有效载荷中的属性创建新模型。等效的简写为
this.post("/movies")
您可以在下面看到可用简写的完整列表。简写使用基于 HTTP 动词的默认状态代码
- GET、PATCH/PUT 和 DEL 为 200
- POST 为 201
GET 简写
获取集合
// Shorthand
this.get("/contacts") // finds type by singularizing url
this.get("/contacts", "users") // optionally specify the collection as second param
// equivalent
this.get("/contacts", (schema) => {
return schema.contacts.all() // users in the second case
})
获取模型
// Shorthand
this.get("/contacts/:id") // finds type by singularizing url
this.get("/contacts/:id", "user") // optionally specify the type as second param
// equivalent
this.get("/contacts/:id", (schema, request) => {
let id = request.params.id
return schema.contacts.find(id) // users in the second case
})
按 ID 获取多个模型(例如,GET /contacts?ids=1,3
)
// Shorthand
this.get("/contacts", { coalesce: true })
this.get("/contacts", "users", { coalesce: true })
// equivalent
this.get("/contacts", ({ contacts }, request) => {
let ids = request.queryParams.ids
return contacts.find(ids) // users in the second case
})
POST 简写
创建资源
// Shorthand
this.post("/contacts") // finds type by singularizing url
this.post("/contacts", "user") // optionally specify the type as second param
// equivalent
this.post("/contacts", function (schema, request) {
let attrs = this.normalizedRequestAttrs()
return schema.contacts.create(attrs)
})
为了使此 POST 简写正常工作,Mirage 需要知道您的应用程序随请求一起发送的 JSON 有效载荷的格式,以便它可以将适当的数据插入数据库。请参阅 序列化器文档中关于 normalize 的说明,以获取更多信息。
PATCH/PUT 简写
更新资源
// Shorthand (these also work with this.put)
this.patch("/contacts/:id") // finds type by singularizing url
this.patch("/contacts/:id", "user") // optionally specify the type as second param
// equivalent
this.patch("/contacts/:id", function (schema, request) {
let id = request.params.id
let attrs = this.normalizedRequestAttrs()
return schema.contacts.find(id).update(attrs)
})
为了使此 PATCH 简写正常工作,Mirage 需要知道您的应用程序随请求一起发送的 JSON 有效载荷的格式,以便它可以将适当的数据插入数据库。请参阅 序列化器文档中关于 normalize 的说明,以获取更多信息。
DELETE 简写
销毁资源
// Shorthand
this.del("/contacts/:id") // finds type by singularizing url
this.del("/contacts/:id", "user") // optionally specify the type as second param
// equivalent
this.del("/contacts/:id", (schema, request) => {
let id = request.params.id
schema.contacts.find(id).destroy()
})
销毁资源和相关模型
// Shorthand
this.del("/contacts/:id", ["contact", "addresses"])
// equivalent
this.del("/contacts/:id", ({ contacts }, request) => {
let id = request.params.id
let contact = contacts.find(id)
contact.addresses.destroy()
contact.destroy()
})
要使用此简写,您必须在数据层中定义适当的 hasMany
/belongsTo
关系。
资源助手
_resource_ 助手允许您为给定资源定义多个简写
// Resource helper usage
this.resource("contacts")
// Shorthands defined
this.get("/contacts")
this.get("/contacts/:id")
this.post("/contacts")
this.patch("/contacts/:id") // and this.put('/contacts/:id')
this.del("/contacts/:id")
您还可以使用 _only_ 选项将定义哪些简写列入白名单
this.resource("contacts", { only: ["index", "show"] })
// Shorthands defined
this.get("/contacts")
this.get("/contacts/:id")
或者使用 _except_ 选项指定哪些路由处理器不应定义
this.resource("contacts", { except: ["update"] })
// Shorthands defined
this.get("/contacts")
this.get("/contacts/:id")
this.post("/contacts")
this.del("/contacts/:id")
如果您的路由路径和集合名称不匹配,您可以使用 _path_ 选项定义相对路径或绝对路径
this.resource("blog-posts", { path: "/posts" })
// Shorthands defined
this.get("/posts", "blog-posts")
this.get("/posts/:id", "blog-posts")
this.post("/posts", "blog-posts")
this.put("/posts/:id", "blog-posts")
this.patch("/posts/:id", "blog-posts")
this.del("/posts/:id", "blog-posts")
以下是您可以传递给 _only_/_except_ 选项以及它们代表的简写操作名称的完整参考
Action | Shorthand
------------------------------
index | this.get('/contacts')
show | this.get('/contacts/:id')
create | this.post('/contacts')
update | this.patch('contacts/:id') (or this.put)
delete | this.del('/contacts/:id')
简写是您在前端代码库中保持高效的关键部分,但它们之所以能够正常工作,是因为 Mirage 拥有一个了解您应用程序域模型的数据层。
我们将在下一节中介绍数据层的核心部分——数据库。