Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] add explicit types to examples #2657

Merged
merged 7 commits into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/poor-cameras-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-svelte': patch
---

Add explicit types in `_api.ts` for TypeScript and ESLint integration example
ignatiusmb marked this conversation as resolved.
Show resolved Hide resolved
14 changes: 12 additions & 2 deletions packages/create-svelte/templates/default/src/routes/todos/_api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Request } from '@sveltejs/kit';
import type { Request, RequestHandler } from '@sveltejs/kit';
import type { Locals } from '$lib/types';

/*
Expand All @@ -14,7 +14,17 @@ import type { Locals } from '$lib/types';

const base = 'https://api.svelte.dev';

export async function api(request: Request<Locals>, resource: string, data?: {}) {
// unwrap possible handlers from RequestHandler return types
type MaybeHandle = Exclude<ReturnType<RequestHandler>, Promise<unknown>>;
// remove void since api will always return a handler
// re-promisify to satisfy async function constraints
type Handler = Promise<Exclude<MaybeHandle, void>>;

export async function api(
request: Request<Locals>,
resource: string,
data?: Record<string, unknown>
): Handler {
dummdidumm marked this conversation as resolved.
Show resolved Hide resolved
// user must have a cookie set
if (!request.locals.userid) {
return { status: 401 };
Expand Down