Skip to content

Commit

Permalink
add implementation, test, and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
wd-David committed Feb 11, 2025
1 parent ba957a3 commit e5b1127
Show file tree
Hide file tree
Showing 10 changed files with 1,507 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/runed/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"@vitest/coverage-v8": "^1.5.1",
"@vitest/ui": "^1.6.0",
"jsdom": "^24.0.0",
"msw": "^2.7.0",
"publint": "^0.1.9",
"resize-observer-polyfill": "^1.5.1",
"svelte": "^5.11.0",
Expand Down
1 change: 1 addition & 0 deletions packages/runed/src/lib/utilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ export * from "./persisted-state/index.js";
export * from "./use-geolocation/index.js";
export * from "./context/index.js";
export * from "./is-in-viewport/index.js";
export * from "./resource/index.js";
1 change: 1 addition & 0 deletions packages/runed/src/lib/utilities/resource/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./resource.svelte.js";
46 changes: 46 additions & 0 deletions packages/runed/src/lib/utilities/resource/msw-handlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { http, delay, HttpResponse } from "msw";

export type ResponseData = {
id: number;
name: string;
email: string;
};

export type SearchResponseData = {
results: { id: number; title: string }[];
page: number;
total: number;
};

export const handlers = [
// Basic user endpoint
http.get("https://api.example.com/users/:id", async ({ params }) => {
await delay(50);
return HttpResponse.json({
id: Number(params.id),
name: `User ${params.id}`,
email: `user${params.id}@example.com`,
});
}),

// Search endpoint with query params
http.get("https://api.example.com/search", ({ request }) => {
const url = new URL(request.url);
const query = url.searchParams.get("q");
const page = Number(url.searchParams.get("page")) || 1;

return HttpResponse.json({
results: [
{ id: page * 1, title: `Result 1 for ${query}` },
{ id: page * 2, title: `Result 2 for ${query}` },
],
page,
total: 10,
});
}),

// Endpoint that can fail
http.get("https://api.example.com/error-prone", () => {
return new HttpResponse(null, { status: 500 });
}),
];
Loading

0 comments on commit e5b1127

Please sign in to comment.