-
Notifications
You must be signed in to change notification settings - Fork 0
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
Antti/api #10
Antti/api #10
Conversation
…test code to gifts.ts
…for GET request and tested getting queries
…nctions for every request type and some basic test code
…jsonServerFunctions.ts to giftRequests.ts for now. Changed baseURL variable's string to use built api. Changed all request functions to work with the built api
…nged the import as well
…t. Prettier also re-positioned code
…/catch blocks to all handles to get correct error messages. Might delete later if not needed
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Pohjustuksena: rupesin tekemään https://nextjs.org/docs/pages/building-your-application/routing/api-routes tämän pohjalla, enkä löytänyt kaikkeen sopivia vastauksia, joten improvisoin. Nähtäväksi jää kuinka paljon menin metsään 😅 |
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.
Vastasin kysymyksiin. Lisäksi pushasin yhden commitin missä laitoin muutamia eslint-sääntöjä päälle jotka oli aiemmin otettu pois. Varoittavat any
n käytöstä mitä on yleensä syytä välttää. Sulla on jo sen verran kokemusta että pärjäät sääntöjen kanssa 🙂
…ed one check instead of every handler having their own check
…ted gift instead of given values of gift that is wanted to be created
…erty from newGift object. Added a variable for createGift function to get the gift data of created gift. Updated updatedGiftList variable to use correct data while concatting
pages/api/gifts/[id].ts
Outdated
try { | ||
const reqHandler = req.method !== undefined && HANDLERS[req.method]; | ||
if (reqHandler) { | ||
if (typeof req.query.id !== 'string') { | ||
throw new Error('Invalid ID', { cause: 'idError' }); | ||
} | ||
|
||
await reqHandler(req, res); |
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.
Tein tämän, jotta jokaisessa Handler-funktiossa ei tarvitse alussa tarkistaa samaa asiaa. Tähän asiaan liittyen yksi kysymys alempana. Käytän tässä myös Errorissa cause
:a. Oma Errori on tehty Postgres-versiossa, en sitä mergennyt tänne
Edit: typo korjattu
pages/api/gifts/[id].ts
Outdated
if (typeof req.query.id !== 'string') { | ||
throw new Error('Invalid ID', { cause: 'idError' }); | ||
} | ||
queryID = req.query.id; | ||
|
||
await reqHandler(req, res); |
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.
Tein tämän queryID
-variablen, koska TypeScript ei tunnista tuota tarkistusta ylempänä. TypeScript ajattelee, että req.query.id
voisi olla string | string[] | undefined
, kun käsittelee handle
-funktioita. Sen ei pitäisi kuitenkaan olla mahdollista, koska handle
-funktiot kutusutaan vain tuon tarkistuksen jälkeen 😄
Olisiko tuohon jotain parempaa tapaa tehdä vai onko ihan ok? 😄
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.
Idea on hyvä välttää toistoa, mutta ei toimi hyvin noin. Globaaleita muuttujia ei kannata käyttää, sillä nyt jos kaksi requestia tulee lähes yhtä aikaa ehtii jälkimmäinen request muuttaa queryID
muuttujan arvon ennen kuin ensimmäinen ehtii käyttää sitä.
Tuo kannattaa tehdä niin, että muuttaa tuon yllä olevan rivin 28 näin: const queryId = req.query.id;
Huomaat että sille tulee tyypiksi string, jonka voi sitten välittää funktion parametrina, eli noille HANDLER-funktioille pitää lisätä kolmas parametri.
Selkeyden vuoksi muuttaisin parametrit objektiksi, jolloin tulisi jotenkin näin
type HandlerParams = {
req: NextApiRequest;
res: NextApiResponse;
queryId: string;
};
const HANDLERS: Record<string, (params: HandlerParams) => Promise<void>> = {
GET: handleGET,
PATCH: handlePATCH,
PUT: handlePUT,
DELETE: handleDELETE,
} as const;
async function handleGET({ req, res, queryId }: HandlerParams) {
const giftRequest = await axios.get(`${baseURL}/${queryId}`);
return res.status(giftRequest.status).send(giftRequest.data as Gift);
}
Ja itse asiassa handleGET
funktiosta voi yltä poistaa req
kun sitä ei tarvita.
… added queryId as a new type. Fixed all the handles to have HandlerParams as type
…s custom message due to delete request doesn't seem to return anything by default
…atedGift variable at pages/index.tsx
pages/api/gifts/[id].ts
Outdated
async function handleDELETE({ res, queryId }: HandlerParams) { | ||
const deleteRequest = await axios.delete(`${baseURL}/${queryId}`); | ||
return res.status(deleteRequest.status).json(req.body); | ||
return res | ||
.status(deleteRequest.status) | ||
.send('Lahja poistettu onnistuneesti!'); | ||
} |
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.
Laitoin tuohon oman tekstin, kun lahja on poistettu, lienee melko turha.
Voisin laittaa kaikki virheet, return-statementistä lähetettävät tiedot yms englanniksi. Kääntää ne vain suomeksi sitten, kun tekee toastin.
Esim. että tuo 'Lahja poistettu onnistuneesti!'
olisikin 'Gift deleted succesfully'
yms
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.
Jep status-tekstit joita ei suoraan näytetä käyttäjälle kannattaa kirjoittaa englanniksi. Tähän en kuitenkaan laittaisi status-tekstiä, sillä yleensä DELETE-request ei palauta mitään. Se että lahjan poisto onnistui kerrotaan status-koodilla (200). Jos poisto ei jostain syystä onnistu palautetaan esim. koodi 400 tai 500.
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.
Joo, näin ajattelinkin 👍
Kaikki pitäisi olla korjattu. Pahoittelut jos ei ollutkaan. Halusin tehdä nämä vielä tänään vaikkakin myöhäiseksi menikin 😄 |
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.
Vielä pienet muutokset.
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.
EDIT: vahingossa valitsin request changes.
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.
Erinomaista työtä! 🔥 Voi mergetä!
Keskeneräinen vielä. Tarkoituksena tehdä REST-apin tyylinen ratkaisu