@codegenapps/frontend-sdk 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +120 -0
- package/dist/CgaClient.d.ts +69 -0
- package/dist/builder/QueryBuilder.d.ts +38 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.esm.js +6409 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.umd.js +6417 -0
- package/dist/index.umd.js.map +1 -0
- package/package.json +40 -0
package/README.md
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# Frontend SDK
|
|
2
|
+
|
|
3
|
+
A dynamic, schema-driven frontend SDK for seamless API integration, featuring a fluent query builder.
|
|
4
|
+
|
|
5
|
+
This SDK is designed to be highly flexible. It dynamically fetches an API specification (like `swagger.json` or `openapi.json`) at runtime and builds a powerful, chainable query interface based on it.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- **Dynamic Schema**: Initializes from a live API documentation URL. No need to rebuild the SDK when the API changes.
|
|
10
|
+
- **Fluent Query Builder**: An intuitive, chainable interface (`from().select().eq()...`) for building complex API requests.
|
|
11
|
+
- **Framework Agnostic**: Works with any frontend framework (React, Vue, Angular) or plain JavaScript/TypeScript.
|
|
12
|
+
- **TypeScript Support**: Provides type definitions for core components, although API response types are dynamic.
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install frontend-sdk # Replace 'frontend-sdk' with your actual package name on npm
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
Here's how to initialize the client and perform a query:
|
|
23
|
+
|
|
24
|
+
```javascript
|
|
25
|
+
import cga from 'frontend-sdk';
|
|
26
|
+
|
|
27
|
+
// 1. Initialize the client
|
|
28
|
+
async function initialize() {
|
|
29
|
+
await cga.init({
|
|
30
|
+
// The base URL of your API
|
|
31
|
+
baseUrl: 'http://localhost:8080/api',
|
|
32
|
+
|
|
33
|
+
// A function that provides your SDK license key
|
|
34
|
+
licenseKeyProvider: () => 'YOUR_SDK_LICENSE_KEY',
|
|
35
|
+
|
|
36
|
+
// (Optional) A function that provides the JWT for authenticated requests
|
|
37
|
+
tokenProvider: () => localStorage.getItem('jwt_token'),
|
|
38
|
+
});
|
|
39
|
+
console.log('SDK Initialized!');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// 2. Perform queries
|
|
43
|
+
async function fetchDocuments() {
|
|
44
|
+
try {
|
|
45
|
+
const { data, error } = await cga
|
|
46
|
+
.from('documents')
|
|
47
|
+
.select('id, title')
|
|
48
|
+
.eq('owner_id', 1)
|
|
49
|
+
.order('created_at', { ascending: false })
|
|
50
|
+
.run();
|
|
51
|
+
|
|
52
|
+
if (error) {
|
|
53
|
+
console.error('Failed to fetch documents:', error);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
console.log('Fetched Documents:', data);
|
|
58
|
+
} catch (err) {
|
|
59
|
+
console.error('An unexpected error occurred:', err);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
initialize().then(fetchDocuments);
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## API
|
|
67
|
+
|
|
68
|
+
### Initialization
|
|
69
|
+
|
|
70
|
+
`cga.init(config)`
|
|
71
|
+
|
|
72
|
+
Must be called once before any other methods.
|
|
73
|
+
|
|
74
|
+
- `config.baseUrl` (string, required): The base URL for your API (e.g., `https://api.example.com/api`). The SDK will automatically try to fetch the schema from `{baseUrl_origin}/swagger/doc.json`.
|
|
75
|
+
- `config.licenseKeyProvider` (Function, required): A function that returns your SDK license key.
|
|
76
|
+
- `config.tokenProvider` (Function, optional): A function that returns the JWT for bearer authentication.
|
|
77
|
+
- `config.headers` (Object, optional): An object of default headers to be sent with every request.
|
|
78
|
+
|
|
79
|
+
### Querying
|
|
80
|
+
|
|
81
|
+
#### `.from(tableName: string)`
|
|
82
|
+
|
|
83
|
+
Starts a query chain for a specific resource/table.
|
|
84
|
+
|
|
85
|
+
#### `.select(fields: string)`
|
|
86
|
+
|
|
87
|
+
Selects which fields to return. If the backend doesn't support field selection, this will be handled client-side.
|
|
88
|
+
|
|
89
|
+
#### Filter Methods
|
|
90
|
+
|
|
91
|
+
- `.eq(column, value)`
|
|
92
|
+
- `.neq(column, value)`
|
|
93
|
+
- `.gt(column, value)`
|
|
94
|
+
- `.gte(column, value)`
|
|
95
|
+
- `.lt(column, value)`
|
|
96
|
+
- `.lte(column, value)`
|
|
97
|
+
- `.like(column, value)`
|
|
98
|
+
- `.in(column, valuesArray)`
|
|
99
|
+
|
|
100
|
+
#### `.byPk(primaryKeyObject)`
|
|
101
|
+
|
|
102
|
+
Selects a single record by its primary key (or composite primary key).
|
|
103
|
+
|
|
104
|
+
Example: `cga.from('daily_active_user').byPk({ activity_date: '...', user_id: 1, ... })`
|
|
105
|
+
|
|
106
|
+
#### Data Modification
|
|
107
|
+
|
|
108
|
+
- `.insert(dataObject | dataArray)`
|
|
109
|
+
- `.update(dataObject)`
|
|
110
|
+
- `.delete()`
|
|
111
|
+
|
|
112
|
+
**Important**: `update` and `delete` must be chained with a filter method (like `.eq()` or `.byPk()`) to specify which record(s) to modify.
|
|
113
|
+
|
|
114
|
+
#### `.run()`
|
|
115
|
+
|
|
116
|
+
Executes the query and returns a `Promise` that resolves to `{ data, error }`. **All query chains must end with `.run()`.**
|
|
117
|
+
|
|
118
|
+
## License
|
|
119
|
+
|
|
120
|
+
MIT
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { QueryBuilder } from './builder/QueryBuilder';
|
|
2
|
+
/**
|
|
3
|
+
* Configuration for the CgaClient.
|
|
4
|
+
*/
|
|
5
|
+
export interface CgaClientConfig {
|
|
6
|
+
/**
|
|
7
|
+
* The base URL of the API.
|
|
8
|
+
* The SDK will attempt to fetch the API documentation from `{baseUrl_origin}/swagger/doc.json`.
|
|
9
|
+
* @example "https://api.example.com/api"
|
|
10
|
+
*/
|
|
11
|
+
baseUrl: string;
|
|
12
|
+
/**
|
|
13
|
+
* A function that returns the license key.
|
|
14
|
+
* This function will be called during initialization to validate the SDK usage.
|
|
15
|
+
*/
|
|
16
|
+
licenseKeyProvider: () => Promise<string> | string;
|
|
17
|
+
/**
|
|
18
|
+
* Optional headers to be sent with every request,
|
|
19
|
+
* e.g., for API keys.
|
|
20
|
+
* @example { 'X-API-KEY': 'YOUR_API_KEY' }
|
|
21
|
+
*/
|
|
22
|
+
headers?: Record<string, string>;
|
|
23
|
+
/**
|
|
24
|
+
* An optional function that provides the JWT token for bearer authentication.
|
|
25
|
+
* This function will be called before each request that requires authentication.
|
|
26
|
+
*/
|
|
27
|
+
tokenProvider?: () => Promise<string | null> | string | null;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The main client for interacting with the API.
|
|
31
|
+
*/
|
|
32
|
+
export declare class CgaClient {
|
|
33
|
+
private schema;
|
|
34
|
+
private httpClient;
|
|
35
|
+
/**
|
|
36
|
+
* Creates a new CgaClient instance.
|
|
37
|
+
* Note: The client is not ready to use until `init()` is called.
|
|
38
|
+
*/
|
|
39
|
+
constructor();
|
|
40
|
+
/**
|
|
41
|
+
* Initializes the client with the provided configuration.
|
|
42
|
+
* This method must be called before any other methods are used.
|
|
43
|
+
* @param config The configuration for the client.
|
|
44
|
+
*/
|
|
45
|
+
init(config: CgaClientConfig): Promise<void>;
|
|
46
|
+
/**
|
|
47
|
+
* Starts a query chain for a specific table.
|
|
48
|
+
* @param tableName The name of the table/resource to query.
|
|
49
|
+
*/
|
|
50
|
+
from(tableName: string): QueryBuilder;
|
|
51
|
+
/**
|
|
52
|
+
* Provides access to authentication-related methods.
|
|
53
|
+
*/
|
|
54
|
+
get auth(): {
|
|
55
|
+
/**
|
|
56
|
+
* Logs in a user.
|
|
57
|
+
* @param username The user's username.
|
|
58
|
+
* @param password The user's password.
|
|
59
|
+
* @param apiKey The API key required for login.
|
|
60
|
+
*/
|
|
61
|
+
login(username: string, password: string, apiKey: string): Promise<{
|
|
62
|
+
data: any;
|
|
63
|
+
error: null;
|
|
64
|
+
} | {
|
|
65
|
+
data: null;
|
|
66
|
+
error: any;
|
|
67
|
+
}>;
|
|
68
|
+
};
|
|
69
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { AxiosInstance, AxiosRequestConfig } from 'axios';
|
|
2
|
+
interface RequestOptions {
|
|
3
|
+
headers?: Record<string, string>;
|
|
4
|
+
axiosConfig?: AxiosRequestConfig;
|
|
5
|
+
}
|
|
6
|
+
export declare class QueryBuilder {
|
|
7
|
+
private state;
|
|
8
|
+
private schema;
|
|
9
|
+
private httpClient;
|
|
10
|
+
constructor(tableName: string, schema: any, httpClient: AxiosInstance);
|
|
11
|
+
private setRequestOptions;
|
|
12
|
+
select(fields: string, options?: RequestOptions): this;
|
|
13
|
+
eq(column: string, value: any): this;
|
|
14
|
+
neq(column: string, value: any): this;
|
|
15
|
+
gt(column: string, value: any): this;
|
|
16
|
+
gte(column: string, value: any): this;
|
|
17
|
+
lt(column: string, value: any): this;
|
|
18
|
+
lte(column: string, value: any): this;
|
|
19
|
+
like(column: string, value: string): this;
|
|
20
|
+
in(column: string, values: any[]): this;
|
|
21
|
+
byPk(keys: Record<string, any>): this;
|
|
22
|
+
range(page: number, pageSize: number): this;
|
|
23
|
+
order(column: string, options?: {
|
|
24
|
+
ascending?: boolean;
|
|
25
|
+
}): this;
|
|
26
|
+
meta(meta: string | string[]): this;
|
|
27
|
+
groupBy(groupBy: string | string[]): this;
|
|
28
|
+
insert(data: any | any[], options?: RequestOptions): this;
|
|
29
|
+
update(data: any, options?: RequestOptions): this;
|
|
30
|
+
delete(data?: any | any[], options?: RequestOptions): this;
|
|
31
|
+
run<TResponse>(): Promise<{
|
|
32
|
+
data: TResponse | TResponse[] | null;
|
|
33
|
+
error: any;
|
|
34
|
+
}>;
|
|
35
|
+
private buildRequestDetails;
|
|
36
|
+
private buildUrl;
|
|
37
|
+
}
|
|
38
|
+
export {};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { CgaClient } from './CgaClient';
|
|
2
|
+
import type { CgaClientConfig } from './CgaClient';
|
|
3
|
+
export type { CgaClientConfig };
|
|
4
|
+
/**
|
|
5
|
+
* The main instance of the CgaClient.
|
|
6
|
+
* Use this to interact with the API.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* import cga from 'your-sdk-name';
|
|
10
|
+
*
|
|
11
|
+
* async function main() {
|
|
12
|
+
* await cga.init({
|
|
13
|
+
* baseUrl: '...',
|
|
14
|
+
* licenseKeyProvider: () => '...',
|
|
15
|
+
* });
|
|
16
|
+
*
|
|
17
|
+
* const { data, error } = await cga.from('documents').select('*');
|
|
18
|
+
* }
|
|
19
|
+
*/
|
|
20
|
+
declare const cga: CgaClient;
|
|
21
|
+
export default cga;
|