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

Export endpoint request type #664

Closed
arnemolland opened this issue Mar 25, 2021 · 3 comments
Closed

Export endpoint request type #664

arnemolland opened this issue Mar 25, 2021 · 3 comments
Labels
feature / enhancement New feature or request help wanted PRs welcomed. The implementation details are unlikely to cause debate

Comments

@arnemolland
Copy link

Is your feature request related to a problem? Please describe.
I'd like for an endpoint request object to be typed equal to RequestHandlerResponse.

Describe the solution you'd like
It's pretty straightforward, though I'm not sure about naming. For reference, Next.js has named their equivalent types NextApiRequest and NextApiResponse.

Describe alternatives you've considered
As of now, I'm declaring the type myself.

How important is this feature to you?
For direct usage of the RequestHandler function, you inherit typings from the function type, but constructing a standalone request object requires custom type declaration which is a pain in comparison with how small of a change it is to export the type.

Additional context
An alternative declaration (following current naming):

export interface RequestHandlerResponse {
	status?: number;
	headers?: Record<string, string>;
	body?: any;
}

export interface RequestHandlerRequest {
	host: string;
	headers: Headers;
	path: string;
	params: Record<string, string>;
	query: URLSearchParams;
	body: string | Buffer | ReadOnlyFormData;
}

export type RequestHandler = (
	request?: RequestHandlerRequest,
	context?: any
) => RequestHandlerResponse | Promise<RequestHandlerResponse>;
@Rich-Harris Rich-Harris added feature / enhancement New feature or request help wanted PRs welcomed. The implementation details are unlikely to cause debate labels Mar 25, 2021
@Rich-Harris
Copy link
Member

Yeah, this makes sense. I'd suggest we keep it simple and just have Request, Response and RequestHandler. These types will likely change if we implement #334 (comment)context will become a property of Request, and RequestHandler will have a single argument — but not in a way that changes the crux of this issue

@ignatiusmb
Copy link
Member

Shouldn't this be resolved already, as Request and Response are now available to import from @sveltejs/kit?

@ignatiusmb
Copy link
Member

RequestHandler now only have a single argument and its own response type

export interface RequestHandler<
Locals = Record<string, any>,
Input = unknown,
Output extends DefaultBody = DefaultBody
> {
(request: ServerRequest<Locals, Input>): MaybePromise<void | EndpointOutput<Output>>;
}

Which is exposed publicly as Request and EndpointOutput respectively

export { EndpointOutput, RequestHandler } from './endpoint';
export { ErrorLoad, ErrorLoadInput, Load, LoadInput, LoadOutput, Page } from './page';
export {
GetSession,
Handle,
ServerFetch,
ServerRequest as Request,
ServerResponse as Response
} from './hooks';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature / enhancement New feature or request help wanted PRs welcomed. The implementation details are unlikely to cause debate
Projects
None yet
Development

No branches or pull requests

3 participants