Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

add types for stores #1568

Closed
wants to merge 5 commits into from
Closed
Changes from 2 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
47 changes: 36 additions & 11 deletions runtime/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,53 @@
declare module "@sapper/app"
declare module "@sapper/server"
declare module "@sapper/service-worker"
declare module "@sapper/app";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there were just removed in #1598. you might need to rebase against master

declare module "@sapper/server";
declare module "@sapper/service-worker";

interface Stores {
preloading: {
subscribe(preloading: boolean): () => void;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are ready-made types for Stores in svelte that you could reuse that give a more complete type definition for subscribe (there's an optional second parameter and a return value that are missing here):

I think if you just do

import { Readable, Writable } from 'svelte/store';

...
	preloading: Readable<boolean>;

you should get the correct definitions for subscribe etc for free.

For the page store, there is also already a PageContext interface, so you could declare it as

	import { PageContext } from '@sapper/app/types';
...
	page: Writeable<PageContext>;

Copy link
Contributor Author

@TheComputerM TheComputerM Sep 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, Thanks.

};
page: {
subscribe(info: {
error: boolean;
host: string;
path: string;
params: object;
query: object;
}): () => void;
};
session: {
set(value: any): void;
update(update: (existing) => {}): void;
subscribe(value: any): () => void;
};
}

declare module "@sapper/app" {
export interface Redirect {
statusCode: number
location: string
statusCode: number;
location: string;
}

export function goto(href: string, opts: { noscroll?: boolean, replaceState?: boolean }): Promise<void>;
export function prefetch(href: string): Promise<{ redirect?: Redirect; data?: unknown }>;
export function goto(
href: string,
opts: { noscroll?: boolean; replaceState?: boolean }
): Promise<void>;
export function prefetch(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you leave the method formatting the way it was?

href: string
): Promise<{ redirect?: Redirect; data?: unknown }>;
export function prefetchRoutes(pathnames: string[]): Promise<void>;
export function start(opts: { target: Node }): Promise<void>;
export const stores: () => unknown;
export function stores(): Stores;
}

declare module "@sapper/server" {
import { Handler, Req, Res } from '@sapper/internal/manifest-server';
import { Handler, Req, Res } from "@sapper/internal/manifest-server";

export type Ignore = string | RegExp | ((uri: string) => boolean) | Ignore[];

export interface MiddlewareOptions {
session?: (req: Req, res: Res) => unknown
ignore?: Ignore
session?: (req: Req, res: Res) => unknown;
ignore?: Ignore;
}

export function middleware(opts: MiddlewareOptions): Handler;
Expand Down