Skip to content

Commit

Permalink
Merge pull request #57 from brandon-schabel/usage-examples
Browse files Browse the repository at this point in the history
Usage examples
  • Loading branch information
brandon-schabel authored Nov 15, 2023
2 parents 0e5bb7c + e4a6469 commit eab89e6
Show file tree
Hide file tree
Showing 6 changed files with 624 additions and 2 deletions.
92 changes: 92 additions & 0 deletions docs/usage/FETCHER-USAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Fetcher Usage Guide

## Overview

`createFetchFactory` is a TypeScript utility designed for making HTTP requests. It abstracts the complexity of fetch requests, providing a more structured and type-safe way to interact with APIs.

### Importing the Necessary Utilities

To get started, ensure you import the following from `create-fetch-factory`:

```typescript
import { createFetchFactory, FactoryMethods } from "@bnk/core/modules/fetcher"
```

### Function Signature

`createFetchFactory` is a generic function with the following signature:

```typescript
export function createFetchFactory<TMap extends TypeMap>({
baseUrl = "",
config,
defaultHeaders,
debug = false,
}: {
baseUrl?: string;
debug?: boolean;
config: Record<keyof TMap, MappedApiConfig<TMap>>;
defaultHeaders?: Headers;
}): ReturnType<typeof createFetchFactory>;
```

### Parameters

1. **baseUrl** (optional): The base URL for the API.
2. **config**: Configuration for API endpoints.
3. **defaultHeaders** (optional): Default headers for all requests.
4. **debug** (optional): Enables debug mode for extra logging.

### Usage

1. **Setting Up**: Define the types for your API endpoints and initialize the fetch factory.

```typescript
type MyApiMap = {
// Define your API endpoints here
};

const fetchFactory = createFetchFactory<MyApiMap>({
baseUrl: 'https://my.api/',
config: {
// API endpoint configurations
},
});
```

2. **Making Requests**: Use the methods provided by the fetch factory to make API requests.

- **GET Request Example**:

```typescript
fetchFactory.get({
endpoint: 'myEndpoint',
// Other configurations
}).then(response => {
// Handle response
});
```

- **POST Request Example**:

```typescript
fetchFactory.post({
endpoint: 'myEndpoint',
body: JSON.stringify({ key: 'value' }),
// Other configurations
}).then(response => {
// Handle response
});
```

### Advanced Features

- **Custom Headers**: You can pass custom headers for each request.
- **Debug Mode**: Enable debug mode for additional logging, useful for development.
- **Type Safety**: Leverage TypeScripts type system for safer and more predictable code.

### Summary

The `createFetchFactory` utility simplifies HTTP requests in TypeScript applications, providing a structured, type-safe way to communicate with APIs. Embrace the power of TypeScript to make your data fetching logic more robust and maintainable!

Happy fetching! 🌐🚀💻
71 changes: 71 additions & 0 deletions docs/usage/FILES-FOLDERS-USAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Files and Folder Module Usage Guide

## Introduction

The Files-Folder module in Bun Nook Kit (BNK) is a versatile suite for managing files and directories in JavaScript applications. It enhances file operations like reading, creating, updating, searching, and validating files and directories. This guide will focus on how this module improves built-in utilities and facilitates frontend references to files in a given directory, including directory search capabilities.

## Features

### File Editing Utilities

- **Save or Update Files**: Easily create or update files with new content.
- **Update Multiple Files**: Update multiple files simultaneously with the same content.

### File Path Utilities

- **Get Full Path**: Retrieve the full path of a file or directory, with an option to resolve relative paths.

### File Reading Utilities

- **List Files and Folders**: Enumerate files and folders in a specified directory.
- **Read Text from Multiple Files**: Asynchronously read text from multiple files.
- **Read JSON**: Efficiently read and parse JSON files.

### File Search Utilities

- **Search Directory for File Name**: Locate files with a specific name within a directory.

### File Validation Utilities

- **File and Directory Existence Check**: Confirm the existence of files and directories.
- **Delete Path**: Safely delete files or directories.
x
## Usage Examples

### Creating or Updating a File

```javascript
import { saveOrUpdateFile } from '@bnk/core/modules/files-folders/file-editing-utils';

await saveOrUpdateFile({
filePath: 'path/to/file.txt',
content: 'Hello World'
});
```

### Reading JSON File

```javascript
import { readJson } from '@bnk/core/modules/files-folders/file-reading-utils';

const data = await readJson('path/to/data.json');
console.log(data);
```

### Searching for Files in a Directory

```javascript
import { searchDirForFileName } from '@bnk/core/modules/files-folders/file-search-utils';

const files = await searchDirForFileName('path/to/directory', 'searchedFileName.txt');
console.log(files);
```

### Checking if a File Exists

```javascript
import { fileExists } from '@bnk/core/modules/files-folders/file-validation-utils';

const exists = await fileExists('path/to/file.txt');
console.log(exists ? 'File exists' : 'File does not exist');
```
2 changes: 0 additions & 2 deletions docs/usage/HTMLODY-USAGE.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
### HTMLody Guide: Building HTML Pages with TypeScript

---

## Overview

HTMLody, part of the `Bun Nook Kit` library, offers a powerful and flexible way to generate HTML content using TypeScript. It allows you to define HTML elements, styles, and content programmatically, making it easier to build both dynamic and static web pages.
Expand Down
Loading

0 comments on commit eab89e6

Please sign in to comment.