Skip to content

Instantly share code, notes, and snippets.

@jrdn91
Created January 14, 2020 15:32
Show Gist options
  • Save jrdn91/812006321a475c7b0f97623fd58aac06 to your computer and use it in GitHub Desktop.
Save jrdn91/812006321a475c7b0f97623fd58aac06 to your computer and use it in GitHub Desktop.
Mirage setup
import { Server, Model, Response } from "miragejs"
import {
consortiaFixtures,
statusFixtures,
typeFixtures,
enrollmentFixtures
} from "./fixtures"
var jwt = require("jsonwebtoken")
export function makeServer({ environment = "development" } = {}) {
let server = new Server({
environment,
seeds(server) {
server.db.loadData({
users: [
{
id: 1,
email: "[email protected]",
name: "Some One",
creator_id: 1,
role: 1,
needs_new_password: 0
}
]
})
},
routes() {
this.namespace = ""
this.urlPrefix = process.env.REACT_APP_API_ENDPOINT
this.post("/users/auth", schema => {
// creates basic token for testing
const token = jwt.sign({}, "shhhhh")
return new Response(
200,
{},
{
token,
user: schema.db.users[0]
}
)
})
this.get("/users/:id", schema => {
return new Response(
200,
{},
{
message: "users get success",
data: schema.db.users[0]
}
)
})
this.put("/users/:id", (schema, request) => {
const user = schema.db.users.findBy({ id: 1 })
const data = JSON.parse(request.requestBody)
schema.db.users[0] = {
...schema.db.users[0],
...data
}
return new Response(
200,
{},
{
message: "users update success",
data: {
...schema.db.users[0],
...data
}
}
)
})
this.get("/memberships/consortia", () => {
return new Response(200, {}, consortiaFixtures)
})
this.get("/memberships/status", () => {
return new Response(200, {}, statusFixtures)
})
this.get("/memberships/type", () => {
return new Response(200, {}, typeFixtures)
})
this.get("/memberships/enrollment", () => {
return new Response(200, {}, enrollmentFixtures)
})
this.passthrough(request => {
console.log(request)
console.log(request.url.match(/pndsn/))
return request.url.match(/pndsn/)
})
}
})
return server
}
@aleyrizvi
Copy link

Perfect, thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment