-
-
Notifications
You must be signed in to change notification settings - Fork 428
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this looks pretty good to me. I wouldn't mind getting someone else to take a look too since we're introducing new API that's exposed to the user
|
||
declare module "@sapper/common" { | ||
export interface PreloadContext { | ||
fetch: (url: string, options?: any) => Promise<any>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be appropriate to use the node-fetch
types here instead of typing opts and res as any?
fetch: (url: RequestInfo, init?: RequestInit) => Promise<Response>;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems like it would probably work for Response
:
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/07b2759140ad390e14801cfcc5ed9ff94182e7bb/types/node-fetch/index.d.ts#L167
For the options, node-fetch
extends the whatwg/fetch standard options with its own:
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/07b2759140ad390e14801cfcc5ed9ff94182e7bb/types/node-fetch/index.d.ts#L46
That would only work on the server and I'm not sure we'd want to expose the underlying implementation to that degree. Perhaps we should just define our own interface for that?
export interface FetchOpts {
body?: BodyInit;
headers?: HeadersInit;
method?: string;
redirect?: RequestRedirect;
signal?: AbortSignal | null;
}
Or maybe we can find types for the browser fetch
that we can use instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I didn't realise node-fetch was a superset of the fetch standard. In that case it's safer to use the equivalent signature for whatwg-fetch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
… in which case I think fetch: WindowOrWorkerGlobalScope["fetch"]
would work
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wondering — why wasn’t this typed in the end?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned below I felt there was no concensus yet on what the typings should be. Feel free to open another PR for a dedicated discussion around this and possibly changing it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would WindowOrWorkerGlobalScope
work on the server-side? I'm not that familiar with typescript, but it sounds like a browser thing
I think this looks good. Apart from the typing of |
It would be good if we could also use these types in the actual sapper code to guarantee that we conform to them. I did a stab at that in this branch and it did reveal that we actually don't :) (it's possible to pass in a I did wind up duplicating @dummdidumm Feel free to merge my branch into this one if you agree with the changes. |
Sorry, one last comment. I find myself unable to use these typings in project code. I set up a TypeScript Sapper project using these instructions, but when importing either I haven't been able to figure out how this is supposed to work. My guess would have been that we need a |
@ehrencrona I had the same issue, I simply had to link the "types": ["svelte", "node", "sapper/runtime"], because this is where the Lines 1 to 3 in e97846c
|
If I remember correctly there's some copy-script that copies these typings into src/node_modules which should make them available. But that's not part of this PR. |
- add `error` to page - make preload function return type `object | Promise<object>`
How to move forward? I updated the PR with all suggestions except the |
@dummdidumm thanks for pushing this through. I just had one more question about naming, but this looks good to me |
Co-authored-by: Ben McCann <[email protected]>
Co-authored-by: Ben McCann <[email protected]>
thanks for all your work on this!! this is a great improvement |
Thank you for releasing the types! I'm getting an error when referencing the import { Page } from '@sapper/common'
export type $page_type = Page;
|
|
||
```html | ||
<script context="module"> | ||
import type { Page, PreloadContext } from "@sapper/common"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry to comment on a merged PR, but both variables doesn't seem to be used below, shouldn't it be Preload
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, yes this is wrong, it should be Preload
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed on master
closes #1463
I added the typings to the
d.ts
file and put it in@sapper/common
which seemed fitting to me since it is used on both client and server. I also added an alternative way of typing the function like @jasonlyu123 suggested, but did not mention it in the docs (I can add that if you want to).I also added a doc entry.