-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from brandon-schabel/usage-examples
Usage examples
- Loading branch information
Showing
6 changed files
with
624 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 TypeScript’s 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! 🌐🚀💻 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.