1.0.0 • Published 7 months ago

neuer-axios-wrapper v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
7 months ago

Neuer Axios Wrapper

Neuer Axios Wrapper is a simple and lightweight JavaScript utility designed to make API requests easy and readable. It is not intended for npm installations but for direct use in the browser with a <script> tag. This utility is perfect for quick prototypes or lightweight applications without complex bundlers or dependency management.

Why Use This Wrapper?

  • Zero Setup: No npm or build steps—just include it with a <script> tag.
  • Human-Readable API Calls: Call your endpoints directly as methods, with automatic URL and options handling.
  • Friendly Debugging: Warns you when something's off and provides error roasting to point you in the right direction.
  • No Bloat: Leverages axios but keeps everything simple and secure with modern JavaScript techniques.

Installation

Add the script directly to your HTML:

<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script src="path/to/neuer-axios-wrapper.js"></script>

Example Usage

Basic Setup

// Initialize the API wrapper
const api = API('https://jsonplaceholder.typicode.com/', {
    headers: { 'Content-Type': 'application/json' },
});

// Use the API
(async () => {
    // GET request to /posts
    const posts = await api.posts();
    console.log(posts);

    // POST request to /posts
    const newPost = await api.posts({
        method: 'POST',
        data: {
            title: 'foo',
            body: 'bar',
            userId: 1,
        },
    });
    console.log(newPost);
})();

Custom Options per Request

You can override defaultOptions for specific requests:

(async () => {
    // GET request with query parameters
    const comments = await api.comments({
        params: { postId: 1 },
    });
    console.log(comments);
})();

Error Handling

The wrapper provides clear and helpful error messages:

  • Missing baseURL:

    🚨 Warning: baseURL is empty! Are you trying to call 'undefined' endpoints? Please fix your baseURL. 🤦‍♂️
  • Request Error:

    💥 API Error: Server returned status 404 for posts.
    👉 Response: { error: 'Not Found' }
  • Network Issues:

    📡 Network Error: No response for posts. Are you sure your server isn't snoozing? 💤
  • Unauthorized Access:

    💥 Unauthorized access: Stop poking around private stuff, noob!

API Reference

API(baseURL, defaultOptions)

  • baseURL: (string) The base URL for all API requests. If empty, a warning is displayed.
  • defaultOptions: (object) Default options passed to Axios for every request.

Returns a proxy object where each property corresponds to an endpoint. You can call the endpoint directly, optionally passing Axios options.

Proxy Behavior

  • api.endpoint(options):
    • options: (object) Overrides for the default Axios options, e.g., method, headers, data, or params.
    • Returns: The response data or null on error.

Example Endpoints

api.posts();        // GET request to /posts
api.posts({         // POST request to /posts
    method: 'POST',
    data: { title: 'New Post' },
});

License

ISC

1.0.0

7 months ago