| 1 |
// Minimum TypeScript Version: 4.2 |
| 2 |
import { |
| 3 |
Response, |
| 4 |
Server, |
| 5 |
JSONAPISerializer, |
| 6 |
createServer, |
| 7 |
Model, |
| 8 |
belongsTo, |
| 9 |
hasMany, |
| 10 |
Factory, |
| 11 |
} from "miragejs"; |
| 12 |
|
| 13 |
export default function config(this: Server): void { |
| 14 |
this.namespace = "foo"; |
| 15 |
this.urlPrefix = "/api"; |
| 16 |
this.timing = 123; |
| 17 |
this.logging = true; |
| 18 |
|
| 19 |
this.get("/foo"); // $ExpectType void |
| 20 |
this.put("/foo"); // $ExpectType void |
| 21 |
this.post("/foo"); // $ExpectType void |
| 22 |
this.patch("/foo"); // $ExpectType void |
| 23 |
this.options("/foo"); // $ExpectType void |
| 24 |
this.del("/foo"); // $ExpectType void |
| 25 |
|
| 26 |
this.get("/foo", () => ({})); |
| 27 |
this.get("/foo", () => 0); |
| 28 |
this.get("/foo", () => "foo"); |
| 29 |
this.get("/foo", () => true); |
| 30 |
this.get("/foo", () => null); |
| 31 |
this.get("/foo", () => ["foo"]); |
| 32 |
this.get("/foo", () => ["foo", 0]); |
| 33 |
this.get<number>("/foo", () => 0); |
| 34 |
this.get<[number, string]>("/foo", () => [0, "foo"]); |
| 35 |
|
| 36 |
this.get<number>("/foo", () => false); // $ExpectError |
| 37 |
this.get<[number, string]>("/foo", () => ["foo", 0]); // $ExpectError |
| 38 |
|
| 39 |
this.resource("foo"); // $ExpectType void |
| 40 |
|
| 41 |
this.passthrough("/_coverage/upload"); // $ExpectType void |
| 42 |
this.passthrough("/_coverage/upload_a", "/_coverage/upload_b"); // $ExpectType void |
| 43 |
this.passthrough(["/_coverage/upload"]); // $ExpectError |
| 44 |
this.passthrough((request) => request.queryParams.skipMirage); // $ExpectType void |
| 45 |
this.passthrough("/_coverage/upload", ["get"]); // $ExpectType void |
| 46 |
// prettier-ignore |
| 47 |
this.passthrough("/_coverage/upload", (request) => request.queryParams.skipMirage); // $ExpectType void |
| 48 |
// prettier-ignore |
| 49 |
this.passthrough("/_coverage/upload", (request) => request.queryParams.skipMirage, ["post"]); // $ExpectType void |
| 50 |
|
| 51 |
this.loadFixtures(); // $ExpectType void |
| 52 |
this.seeds(this); // $ExpectType void |
| 53 |
this.routes(); // $ExpectType void |
| 54 |
|
| 55 |
this.shutdown(); // $ExpectType void |
| 56 |
|
| 57 |
this.get("/test/:segment", (schema, request) => { |
| 58 |
schema.db; // $ExpectType Db |
| 59 |
|
| 60 |
request.params; // $ExpectType Record<string, string> |
| 61 |
request.queryParams; // $ExpectType Record<string, string> |
| 62 |
request.requestBody; // $ExpectType string |
| 63 |
request.requestHeaders; // $ExpectType Record<string, string> |
| 64 |
request.url; // $ExpectType string |
| 65 |
|
| 66 |
return new Response(200, { "Content-Type": "application/json" }, "{}"); |
| 67 |
}); |
| 68 |
|
| 69 |
this.get("/test/:segment", (schema) => Promise.resolve(schema.create("foo"))); // $ExpectType void |
| 70 |
} |
| 71 |
|
| 72 |
// In `new Server`, models and factories are untyped, and you can |
| 73 |
// therefore use any model names you want when interacting with the |
| 74 |
// schema; the return values just won't necessarily have a lot of |
| 75 |
// type information. |
| 76 |
new Server({ |
| 77 |
serializers: { |
| 78 |
application: JSONAPISerializer, |
| 79 |
}, |
| 80 |
fixtures: { |
| 81 |
countries: [ |
| 82 |
{ id: 1, name: "China" }, |
| 83 |
{ id: 2, name: "India" }, |
| 84 |
{ id: 3, name: "United States" }, |
| 85 |
], |
| 86 |
}, |
| 87 |
routes() { |
| 88 |
this.namespace = "api"; |
| 89 |
|
| 90 |
this.schema.all("asfd"); |
| 91 |
|
| 92 |
this.get("/todos", () => { |
| 93 |
return { |
| 94 |
todos: [{ id: "1", text: "Migrate to TypeScript", isDone: false }], |
| 95 |
}; |
| 96 |
}); |
| 97 |
}, |
| 98 |
|
| 99 |
baseConfig() { |
| 100 |
this.pretender.handledRequest = (verb, path, request) => {}; |
| 101 |
this.get("/contacts", () => { |
| 102 |
return ["Interstellar", "Inception", "Dunkirk"]; |
| 103 |
}); |
| 104 |
}, |
| 105 |
|
| 106 |
testConfig() { |
| 107 |
this.namespace = "/test-api"; |
| 108 |
this.get("/movies"); |
| 109 |
this.post("/movies"); |
| 110 |
}, |
| 111 |
}); |
| 112 |
|
| 113 |
// In contrast to `new Server`, `createServer` is able to infer |
| 114 |
// type info from the models and factories you pass it |
| 115 |
createServer({ |
| 116 |
models: { |
| 117 |
pet: Model.extend({ |
| 118 |
owner: belongsTo("person"), |
| 119 |
}), |
| 120 |
person: Model.extend({ |
| 121 |
children: hasMany("person"), |
| 122 |
friends: hasMany<"person" | "pet">({ polymorphic: true }), |
| 123 |
}), |
| 124 |
}, |
| 125 |
|
| 126 |
factories: { |
| 127 |
pet: Factory.extend({ |
| 128 |
name: (n: number) => `Pet ${n}`, |
| 129 |
}), |
| 130 |
person: Factory.extend({ |
| 131 |
name: (n: number) => `Pet ${n}`, |
| 132 |
}), |
| 133 |
}, |
| 134 |
|
| 135 |
routes() { |
| 136 |
this.get("people/:id", (schema, request) => { |
| 137 |
let person = this.schema.find("person", request.params.id); |
| 138 |
|
| 139 |
person?.name; // $ExpectType string | undefined |
| 140 |
|
| 141 |
let friend = person?.friends.models[0]; |
| 142 |
|
| 143 |
friend?.name; // $ExpectType string | undefined |
| 144 |
friend?.children; // $ExpectError |
| 145 |
|
| 146 |
if (friend && "friends" in friend) { |
| 147 |
friend.children.modelName; // $ExpectType string |
| 148 |
} |
| 149 |
|
| 150 |
return person ?? new Response(404); |
| 151 |
}); |
| 152 |
|
| 153 |
this.get("bad", () => { |
| 154 |
return this.schema.all("typo"); // $ExpectError |
| 155 |
}); |
| 156 |
}, |
| 157 |
}); |