Skip to content

Conversation

@danybeltran
Copy link
Member

Feat: HttpReact client (works with sever components)

  • Adds the HttpReact object, which can be used to make requests imperatively. It has a method for each HTTP verb, like get, post, etc. It also has an extend method, similar to axios.create

Example with a sever component in Next.js:

import { HttpReact } from 'http-react'
import { cookies } from 'next/headers'

export default async function MyServerPage() {
  const session = cookies().get('appSession')?.value

  const { data, error } = await HttpReact.get('/api/auth', {
    headers: { Authorization: 'Token ' + session }
  })

  if (!data || error) return <Login />

  return <App />
}

Using .extend:

import { HttpReact } from 'http-react'

const client = HttpReact.extend({
  baseUrl: '/api',
  headers: {
    Authorization: 'Token'
  }
})

export default async function MyServerPage() {
  const { data, error } = await client.get('/auth')

  if (!data || error) return <Login />

  return <App />
}

This also works in client-side components and with getServerSideProps

- Adds the HttpReact object, which can be used to make requests imperatively. It has a method for each HTTP verb, like 'get', 'post', etc. It also has a 'extend' method, similar to 'Axios.create'
@danybeltran danybeltran merged commit 1954122 into atomic-state:master Jan 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant