Skip to content

Instantly share code, notes, and snippets.

View afraser's full-sized avatar

Adam Fraser afraser

  • Rocket Insights
  • Newburyport, MA
View GitHub Profile
@afraser
afraser / apiclient.js
Created July 8, 2021 15:16
An axios-like wrapper around the fetch api that automatically aborts requests under a duplicate key. Provides an abort helper, and handles auth via aws-amplify.
import Auth from '@aws-amplify/auth'
/**
USAGE:
> api.post(`users/${id}`, { email: 'asdf@asdf.com' })
> api.get('users/${id}')
> api.patch(`users/${id}`, { email: 'adam@asdf.com' })
> api.delete(`users/${id}`)
*/
class ApiClient {
@afraser
afraser / useForm.jsx
Created June 29, 2022 18:52
simple form hook
import { useEffect, useMemo, useState } from 'react'
import {
get, set, every, isPlainObject, cloneDeep, cloneDeepWith, isEqual
} from 'lodash'
import { resolver } from 'js/util/validators'
const parseIntOrNull = (val) => {
const intVal = parseInt(val)
return isNaN(intVal) ? null : intVal
}
@afraser
afraser / api.service.ts
Created July 28, 2022 15:52
Axios api wrapper using CancelTokenSource (Deprecated - see https://axios-http.com/docs/cancellation)
import axios, { CancelTokenSource } from 'axios'
import TokenService from './token.service'
export const api = axios.create({
baseURL: process.env.REACT_APP_API_BASE_URL || '',
withCredentials: true,
})
const requestCancelSourcesByKey: { [cancelKey: string]: CancelTokenSource } = {}
@afraser
afraser / api-client.js
Last active July 28, 2022 16:01
api client that makes fetch work more like axios + cognito for auth
import Auth from '@aws-amplify/auth'
/**
USAGE:
> api.post(`users/${id}`, { email: 'asdf@asdf.com' })
> api.get('users/${id}')
> api.patch(`users/${id}`, { email: 'adam@asdf.com' })
> api.delete(`users/${id}`)
*/
class ApiClient {