1.9.39 • Published 12 months ago

nate-react-api-helpers v1.9.39

Weekly downloads
104
License
ISC
Repository
github
Last release
12 months 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 "@material-ui/core";

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.9

12 months ago

1.10.8

12 months ago

1.9.39

12 months ago

1.9.38

12 months ago

1.10.7

3 years ago

1.10.5

3 years ago

1.10.6

3 years ago

1.10.2

3 years ago

1.10.1

3 years ago

1.9.37

4 years ago

1.9.36

4 years ago

1.9.35

4 years ago

1.9.34

4 years ago

1.9.33

4 years ago

1.9.32

4 years ago

1.9.31

4 years ago

1.9.29

4 years ago

1.9.30

4 years ago

1.9.28

4 years ago

1.9.27

4 years ago

1.22.11

4 years ago

1.9.26

4 years ago

1.9.25

4 years ago

1.9.23

4 years ago

1.9.22

4 years ago

1.9.21

4 years ago

1.9.20

4 years ago

1.9.19

4 years ago

1.9.18

4 years ago

1.9.17

4 years ago

1.9.16

4 years ago

1.9.15

4 years ago

1.9.14

4 years ago

1.9.13

4 years ago

1.9.12

4 years ago

1.9.11

4 years ago

1.9.10

4 years ago

1.9.9

4 years ago

1.9.8

4 years ago

1.9.7

4 years ago

1.9.6

4 years ago

1.9.5

4 years ago

1.9.4

4 years ago

1.9.3

4 years ago

1.9.2

4 years ago

1.9.1

4 years ago

1.9.0

4 years ago

1.8.10

4 years ago

1.8.7

4 years ago

1.8.6

4 years ago

1.8.5

4 years ago

1.8.4

4 years ago

1.8.2

4 years ago

1.8.3

4 years ago

1.8.0

5 years ago

1.7.16

5 years ago

1.7.14

5 years ago

1.7.15

5 years ago

1.7.13

5 years ago

1.7.12

5 years ago

1.7.11

5 years ago

1.7.10

5 years ago

1.7.9

5 years ago

1.7.8

5 years ago

1.7.7

5 years ago

1.7.5

5 years ago

1.7.4

5 years ago

1.7.3

5 years ago

1.7.2

5 years ago

1.7.1

5 years ago

1.7.0

5 years ago

1.6.11

5 years ago

1.6.10

5 years ago

1.6.9

5 years ago

1.6.8

5 years ago

1.6.7

5 years ago

1.6.6

5 years ago

1.6.5

5 years ago

1.6.4

5 years ago

1.6.3

5 years ago

1.5.5

5 years ago

1.5.4

5 years ago

1.6.2

5 years ago

1.5.3

5 years ago

1.6.1

5 years ago

1.6.0

5 years ago

1.5.2

5 years ago

1.5.1

5 years ago

1.5.0

5 years ago

1.4.5

5 years ago

1.4.4

5 years ago

1.4.3

5 years ago

1.4.2

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago