ショートハンド
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、DELETEは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にはアプリケーションのドメインモデルを認識するデータレイヤーがあるため、それだけうまく機能します。
次のセクションでは、データレイヤーの重要な部分であるデータベースについて説明します。