Skip to content

Instantly share code, notes, and snippets.

@eblind39
eblind39 / msw-values-from-reqbody.tsx
Created August 14, 2022 02:21
Mock Service Worker - get values from request body
server.use(
rest.post('/login', (req, res, ctx) => {
const {email, password} = req.body as DefaultBodyType & {
email: string
password: string
}
if (
email === '[email protected]' &&
password === 'Aa12345678$'
@eblind39
eblind39 / ts-try-catch-fetch.tsx
Last active August 14, 2022 01:53
ReactJS & Typescript - try catch fetch
try {
setIsFetching(true)
const apiURL = `${API_URL}/authentication/signin`
const strBody: BodyInit = `{"email":"${email}","password":"${password}"}`
const response: Response = await fetch(apiURL, {
method: 'POST',
mode: 'cors',
headers: {
@eblind39
eblind39 / doSignIn-inThunk.ts
Last active August 14, 2022 01:54
Redux toolkit, do signIn via thunk & save JWT in cookie
export const signIn = createAsyncThunk(
'authentication/signin',
async ({email, password}: thunkArgs) => {
const apiURL = `${API_URL}/authentication/signin`
const strBody: BodyInit = `{"email":"${email}","password":"${password}"}`
const response = await fetch(apiURL, {
method: 'POST',
mode: 'cors',
headers: {
@eblind39
eblind39 / form_handlesumbit.tsx
Last active August 14, 2022 01:55
ReactJS & Typescript - Get elements from form in handle submit
import React, {SyntheticEvent} from 'react'
const Login = () => {
const handleSubmit = async (evt: SyntheticEvent) => {
evt.preventDefault()
const target = evt.target as HTMLFormElement
const elements = target.elements as typeof target.elements & {
email: {value: string}
password: {value: string}