Skip to content

Instantly share code, notes, and snippets.

@AllGistsEqual
Created February 22, 2021 20:04
Show Gist options
  • Save AllGistsEqual/eff869407e1d6d516dcc0edd6b1ddec7 to your computer and use it in GitHub Desktop.
Save AllGistsEqual/eff869407e1d6d516dcc0edd6b1ddec7 to your computer and use it in GitHub Desktop.
// File: src/redux/ducks/api.ts
import { Middleware, Action } from 'redux'
import { createAction } from '@reduxjs/toolkit'
const API_HOST = 'http://localhost:3031'
export type SuccessAction<T> = (data: T) => Action
export type ErrorAction = (message: string) => Action
export type ApiBaseRequest = {
url: string
headers?: Record<string, string>
}
export type ApiGetRequest = ApiBaseRequest & {
method: 'GET'
}
export type ApiPostRequest = ApiBaseRequest & {
method: 'POST'
data: Record<string, unknown>
}
export type ApiPutRequest = ApiBaseRequest & {
method: 'PUT'
data: Record<string, unknown>
}
export type ApiDeleteRequest = ApiBaseRequest & {
method: 'DELETE'
}
export type ApiRequest = ApiGetRequest | ApiPostRequest | ApiPutRequest | ApiDeleteRequest
export type ApiRequestPayload<T = never> = ApiRequest & {
onSuccess: SuccessAction<T>
onError: ErrorAction
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment