簡寫

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 酬載格式,才能將適當的資料插入資料庫。如需更多資訊,請參閱序列化器文件中的關於正規化的附註

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 酬載格式,才能將適當的資料插入資料庫。如需更多資訊,請參閱序列化器文件中的關於正規化的附註

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 選項來設定要定義哪些簡寫 (Shorthands)

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 有一個資料層,可以感知您的應用程式的領域模型。

我們將在下一節中介紹資料層的關鍵部分 — 資料庫。