1.10.7 • Published 2 years ago

nate-react-api-helpers v1.10.7

Weekly downloads
104
License
ISC
Repository
github
Last release
2 years ago

Nate's React API Helpers

Installation

npm install nate-react-api-helpers
// or
yarn add nate-react-api-helpers

Usage

  1. Setup your API endpoints
import {APIBase} from "nate-react-api-helpers/APIBase";

class API extends APIBase {
    getCustomers(input: {limit?: number}) {
        return this.fetcher.get<Customer[]>("/api/customers", input);
    }
}

interface Customer {
    id: number;
}

export const api = new API();
  1. Setup auth provider (optional if you use {withoutAuth: true} on useAsync)
import {api} from "../api/API";
import {AuthProvider, useAsyncAction} from "nate-react-api-helpers/Auth"; import {useAuthenticated} from "./Auth";

function App() {
    return (<AuthProvider>
        <LoginModal />
</AuthProvider>)
}

function LoginModal() {
    const auth = useAuthenticated();
    const login = useAsyncAction(async (input) => {
        if(await api.login(input)) {
            auth.setAuthenticated(true);
        }
        // ...
    }, []);
    

    return (
        <Dialog>
            {/* .. inputs .. */}
            <Button onClick={() => login.callback({username, password})}>
        </Dialog>
    );
}
  1. Use api endpoints
import {useAsync} from "nate-react-api-helpers/AsyncUtils";
import {api} from "../api/API";
import React from "react";
import {Grid} from "@mui/material";

export function Customers() {
    const customers = useAsync(() => api.getCustomers());

    // will show as loading while we aren't authenticated
    // after useAuthenticated().setAuthenticated(true), this will 
    // automatically re-fetch and resolve normally
    if(customers.loadingOrError) {
        return customers.LoadingOrErrorElement;
    }
    
    return (
        <Grid container direction="row" spacing={2}>
            {(customers.result || []).map(p => <Grid item key={p.id}>
                {p.name}
            </Grid>)}
        </Grid>
    )
}

export function PublicProducts() {
    const products = useAsync(() => api.getProducts(), {withoutAuth: true});
    if(products.loadingOrError) {
        return products.LoadingOrErrorElement;
    }

    return (
        <Grid container direction="row" spacing={2}>
            {(products.result || []).map(p => <Grid item key={p.id}>
                {p.name}
            </Grid>)}
        </Grid>
    )
}

FAQ

crypto.getRandomValues() not supported...

  • Ensure you've imported react-native-get-random-values prior to importing any API-related files from this package
1.10.7

2 years ago

1.10.5

2 years ago

1.10.6

2 years ago

1.10.2

2 years ago

1.10.1

2 years ago

1.9.37

3 years ago

1.9.36

3 years ago

1.9.35

3 years ago

1.9.34

3 years ago

1.9.33

3 years ago

1.9.32

3 years ago

1.9.31

3 years ago

1.9.29

3 years ago

1.9.30

3 years ago

1.9.28

3 years ago

1.9.27

3 years ago

1.22.11

3 years ago

1.9.26

3 years ago

1.9.25

3 years ago

1.9.23

3 years ago

1.9.22

3 years ago

1.9.21

3 years ago

1.9.20

3 years ago

1.9.19

3 years ago

1.9.18

3 years ago

1.9.17

3 years ago

1.9.16

3 years ago

1.9.15

3 years ago

1.9.14

3 years ago

1.9.13

3 years ago

1.9.12

3 years ago

1.9.11

3 years ago

1.9.10

3 years ago

1.9.9

3 years ago

1.9.8

3 years ago

1.9.7

3 years ago

1.9.6

3 years ago

1.9.5

3 years ago

1.9.4

3 years ago

1.9.3

3 years ago

1.9.2

3 years ago

1.9.1

3 years ago

1.9.0

3 years ago

1.8.10

3 years ago

1.8.7

3 years ago

1.8.6

3 years ago

1.8.5

3 years ago

1.8.4

3 years ago

1.8.2

3 years ago

1.8.3

3 years ago

1.8.0

3 years ago

1.7.16

3 years ago

1.7.14

3 years ago

1.7.15

3 years ago

1.7.13

3 years ago

1.7.12

3 years ago

1.7.11

3 years ago

1.7.10

3 years ago

1.7.9

3 years ago

1.7.8

3 years ago

1.7.7

3 years ago

1.7.5

3 years ago

1.7.4

3 years ago

1.7.3

3 years ago

1.7.2

3 years ago

1.7.1

4 years ago

1.7.0

4 years ago

1.6.11

4 years ago

1.6.10

4 years ago

1.6.9

4 years ago

1.6.8

4 years ago

1.6.7

4 years ago

1.6.6

4 years ago

1.6.5

4 years ago

1.6.4

4 years ago

1.6.3

4 years ago

1.5.5

4 years ago

1.5.4

4 years ago

1.6.2

4 years ago

1.5.3

4 years ago

1.6.1

4 years ago

1.6.0

4 years ago

1.5.2

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.4.5

4 years ago

1.4.4

4 years ago

1.4.3

4 years ago

1.4.2

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago