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/define type for json responses #1113

Closed
Closed
Changes from all 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
4 changes: 2 additions & 2 deletions components/homepage/social-proof-section.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Image from 'next/image';
import { type FC } from 'react';
import { safeResJson } from '~/src/helpers/http';
import { type JsonResponseType, safeResJson } from '~/src/helpers/http';

async function fetchSafe(endpoint: string): Promise<any> {
async function fetchSafe(endpoint: string): Promise<JsonResponseType> {
return safeResJson(await fetch(endpoint));
}

6 changes: 5 additions & 1 deletion src/helpers/http.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export const safeResJson = (res: Response) => {
export interface JsonResponseType {
Copy link
Collaborator

Choose a reason for hiding this comment

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

thing is, we don't always know if the response is a plain Record, basically it can be anything.
I left it any because I needed to strongly type the responses of all API calls and I just didn't feel like doing it, derp.
Normally it should be typed as Promise<unknown> and unknown also extends T, which comes from the place where the function is fired.

@rluders what do u think about this? should we just merge the result to be "fale always" a Record?

[key: string]: string
}

export const safeResJson = (res: Response): Promise<JsonResponseType> => {
if (res.ok) {
return res.json();
}