From 30fa099a760b8682025e6814c9c750b22be693c0 Mon Sep 17 00:00:00 2001 From: towhid Date: Mon, 4 Mar 2024 17:20:25 +0600 Subject: [PATCH] generated docs for List and Show method --- dist/index.cjs | 20 ++++++- dist/index.cjs.map | 2 +- dist/index.esm.js | 20 ++++++- dist/index.esm.js.map | 2 +- dist/index.js | 20 ++++++- dist/index.js.map | 2 +- dist/index.mjs | 20 ++++++- dist/index.mjs.map | 2 +- dist/index.umd.js | 20 ++++++- dist/index.umd.js.map | 2 +- dist/index.umd.min.js | 4 +- dist/index.umd.min.js.map | 2 +- docs/classes/PrestApiClient.md | 19 ++++--- docs/interfaces/PrestApiClientOptions.md | 8 +-- src/api/client.ts | 72 ++++++++++++------------ types/api/client.d.ts | 34 +++++++++++ types/api/client.d.ts.map | 2 +- 17 files changed, 188 insertions(+), 63 deletions(-) diff --git a/dist/index.cjs b/dist/index.cjs index 7e0cf1c..3f77fe3 100644 --- a/dist/index.cjs +++ b/dist/index.cjs @@ -1,5 +1,5 @@ /*! - * prest-client v0.0.4 + * prest-client v0.0.5 * (c) pgEdge * Released under the MIT License. */ @@ -164,6 +164,24 @@ var PrestApiClient = /** @class */ (function () { } }); }); }, + Show: function () { return __awaiter(_this, void 0, void 0, function () { + var response, error_2; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + _a.trys.push([0, 3, , 4]); + return [4 /*yield*/, this.client.get("".concat(this.options.base_url, "/show/").concat(this.database, "/").concat(schemaName, "/").concat(tableName))]; + case 1: + response = _a.sent(); + return [4 /*yield*/, response.json()]; + case 2: return [2 /*return*/, _a.sent()]; + case 3: + error_2 = _a.sent(); + throw new Error("Failed to show data for ".concat(tableName, ": ").concat(error_2.message)); + case 4: return [2 /*return*/]; + } + }); + }); }, }; }; Object.defineProperty(PrestApiClient.prototype, "database", { diff --git a/dist/index.cjs.map b/dist/index.cjs.map index 948c1ff..8cb1a39 100644 --- a/dist/index.cjs.map +++ b/dist/index.cjs.map @@ -1 +1 @@ -{"version":3,"file":"index.cjs","sources":["../src/api/client.ts"],"sourcesContent":["/**\n * Options for creating a Prest API client.\n *\n * @export\n * @interface PrestApiClientOptions\n */\nexport interface PrestApiClientOptions {\n /**\n * The base URL of the Prest API endpoint.\n */\n base_url: string;\n\n /**\n * The username for authentication with the Prest API.\n */\n user_name: string;\n\n /**\n * The password for authentication with the Prest API.\n */\n password: string;\n\n /**\n * The name of the database to connect to.\n */\n database: string;\n}\n\n/**\n * A client for interacting with a Prest API.\n *\n * @export\n * @class PrestApiClient\n */\nexport class PrestApiClient {\n /**\n * The underlying HTTP client for making requests to the Prest API.\n */\n private client:\n | undefined\n | {\n get: (url: string) => Promise;\n };\n\n /**\n * The options used to configure the client.\n */\n private options: PrestApiClientOptions;\n\n /**\n * Creates a new Prest API client with the provided options.\n *\n * @param options - The options for creating the client.\n */\n constructor(options: PrestApiClientOptions) {\n this.options = options;\n this.createClient();\n }\n\n /**\n * Creates the underlying HTTP client with the necessary authentication headers.\n */\n private async createClient() {\n try {\n const username = this.options.user_name;\n const password = this.options.password;\n const authHeader = 'Basic ' + btoa(username + ':' + password);\n\n this.client = {\n get: async (url: string) => {\n const response = await fetch(url, {\n headers: {\n Authorization: authHeader,\n },\n });\n\n if (!response.ok) {\n throw new Error(`Failed to fetch data: ${response.statusText}`);\n }\n\n return response;\n },\n };\n } catch (error) {\n console.error('Error creating client:', error);\n }\n }\n\n /**\n * Returns an object for interacting with a specific table in the database.\n *\n * @param tableName - The name of the table.\n * @returns An object with methods for interacting with the table.\n */\n Table(tableName: string | undefined): { List: () => Promise } {\n if (!this.client) {\n throw new Error('Client not initialized');\n }\n\n if (!tableName) {\n throw new Error('Table name is required');\n }\n\n let schemaName: string | undefined;\n if (tableName.includes('.')) {\n const parts = tableName.split('.');\n schemaName = parts[0];\n tableName = parts[1];\n } else {\n schemaName = 'public';\n }\n\n return {\n List: async () => {\n try {\n const response = await this.client!.get(\n `${this.options.base_url}/${this.database}/${schemaName}/${tableName}`,\n );\n return await response.json();\n } catch (error: any) {\n throw new Error(\n `Failed to fetch data from ${tableName}: ${error.message}`,\n );\n }\n },\n };\n }\n\n /**\n * Gets the name of the database to which the client is connected.\n */\n get database(): string {\n return this.options.database;\n }\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA;;;;;AAKG;AACH,IAAA,cAAA,kBAAA,YAAA;AAeE;;;;AAIG;AACH,IAAA,SAAA,cAAA,CAAY,OAA8B,EAAA;AACxC,QAAA,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,YAAY,EAAE,CAAC;KACrB;AAED;;AAEG;AACW,IAAA,cAAA,CAAA,SAAA,CAAA,YAAY,GAA1B,YAAA;;;;;AACE,gBAAA,IAAI;AACI,oBAAA,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC;AAClC,oBAAA,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC;oBACjC,YAAa,GAAA,QAAQ,GAAG,IAAI,CAAC,QAAQ,GAAG,GAAG,GAAG,QAAQ,CAAC,CAAC;oBAE9D,IAAI,CAAC,MAAM,GAAG;wBACZ,GAAG,EAAE,UAAO,GAAW,EAAA,EAAA,OAAA,SAAA,CAAA,KAAA,EAAA,KAAA,CAAA,EAAA,KAAA,CAAA,EAAA,YAAA;;;;4CACJ,OAAM,CAAA,CAAA,YAAA,KAAK,CAAC,GAAG,EAAE;AAChC,4CAAA,OAAO,EAAE;AACP,gDAAA,aAAa,EAAE,YAAU;AAC1B,6CAAA;AACF,yCAAA,CAAC,CAAA,CAAA;;AAJI,wCAAA,QAAQ,GAAG,EAIf,CAAA,IAAA,EAAA,CAAA;AAEF,wCAAA,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE;4CAChB,MAAM,IAAI,KAAK,CAAC,wBAAA,CAAA,MAAA,CAAyB,QAAQ,CAAC,UAAU,CAAE,CAAC,C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AAA,CAAA,MAAA,CAAA,SAAS,CAAE,CACvE,CAAA,CAAA;;AAFK,4BAAA,QAAQ,GAAG,EAEhB,CAAA,IAAA,EAAA,CAAA;AACM,4BAAA,OAAA,CAAA,CAAA,YAAM,QAAQ,CAAC,IAAI,EAAE,CAAA,CAAA;AAA5B,wBAAA,KAAA,CAAA,EAAA,OAAA,CAAA,CAAA,aAAO,SAAqB,CAAC,CAAA;;;4BAE7B,MAAM,IAAI,KAAK,CACb,4BAA6B,CAAA,MAAA,CAAA,SAAS,EAAK,IAAA,CAAA,CAAA,MAAA,CAAA,OAAK,CAAC,OAAO,CAAE,CAC3D,CAAC;;;;AAEL,aAAA,CAAA,CAAA,EAAA;SACF,CAAC;KACH,CAAA;AAKD,IAAA,MAAA,CAAA,cAAA,CAAI,cAAQ,CAAA,SAAA,EAAA,UAAA,EAAA;AAHZ;;AAEG;AACH,QAAA,GAAA,EAAA,YAAA;AACE,YAAA,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC;SAC9B;;;AAAA,KAAA,CAAA,CAAA;IACH,OAAC,cAAA,CAAA;AAAD,CAAC,EAAA;;;;"} \ No newline at end of file +{"version":3,"file":"index.cjs","sources":["../src/api/client.ts"],"sourcesContent":["/**\n * Options for creating a Prest API client.\n *\n * @export\n * @interface PrestApiClientOptions\n */\nexport interface PrestApiClientOptions {\n /**\n * The base URL of the Prest API endpoint.\n */\n base_url: string;\n\n /**\n * The username for authentication with the Prest API.\n */\n user_name: string;\n\n /**\n * The password for authentication with the Prest API.\n */\n password: string;\n\n /**\n * The name of the database to connect to.\n */\n database: string;\n}\n\n/**\n * A client for interacting with a Prest API.\n *\n * @export\n * @class PrestApiClient\n */\nexport class PrestApiClient {\n /**\n * The underlying HTTP client for making requests to the Prest API.\n */\n private client:\n | undefined\n | {\n get: (url: string) => Promise;\n };\n\n /**\n * The options used to configure the client.\n */\n private options: PrestApiClientOptions;\n\n /**\n * Creates a new Prest API client with the provided options.\n *\n * @param options - The options for creating the client.\n */\n constructor(options: PrestApiClientOptions) {\n this.options = options;\n this.createClient();\n }\n\n /**\n * Creates the underlying HTTP client with the necessary authentication headers.\n */\n private async createClient() {\n try {\n const username = this.options.user_name;\n const password = this.options.password;\n const authHeader = 'Basic ' + btoa(username + ':' + password);\n\n this.client = {\n get: async (url: string) => {\n const response = await fetch(url, {\n headers: {\n Authorization: authHeader,\n },\n });\n\n if (!response.ok) {\n throw new Error(`Failed to fetch data: ${response.statusText}`);\n }\n\n return response;\n },\n };\n } catch (error) {\n console.error('Error creating client:', error);\n }\n }\n\n /**\n * Returns an object for interacting with a specific table in the database.\n *\n * @param tableName - The name of the table.\n * @returns An object with methods for interacting with the table.\n */\n Table(tableName: string | undefined): {\n /**\n * Retrieves the structure of the specified table.\n *\n * @returns A promise that resolves with the table structure.\n * @throws An error if fetching the table structure fails.\n *\n * @example\n * const response = await client.Table('user').List();\n * // Queries the rows of the 'user' table. Public schema is used by default.\n * // Executes GET `/:database/:schema/:table`.\n *\n * @example\n * const response = await client.Table('private.user').List();\n * // Retrieves the rows of the 'user' table in the 'private' schema.\n * // Executes GET `/:database/:schema/:table`.\n *\n * @example\n * const response = await client.Table('public.').List();\n * // Retrieves a list of tables in the 'public' schema.\n * // Executes GET `/:database/:schema`.\n * // Note: The dot at the end is to ignore the table name.\n */\n List: () => Promise;\n\n /**\n * Retrieves data from the specified table.\n *\n * @returns A promise that resolves with the data from the table.\n * @throws An error if fetching data from the table fails.\n *\n * @example\n * const response = await client.Table('user').Show();\n * // Retrieves data from the 'categories' table.\n * // Executes GET `/show/:database/:schema/:table`.\n */\n Show: () => Promise;\n } {\n if (!this.client) {\n throw new Error('Client not initialized');\n }\n\n if (!tableName) {\n throw new Error('Table name is required');\n }\n\n let schemaName: string | undefined;\n if (tableName.includes('.')) {\n const parts = tableName.split('.');\n schemaName = parts[0];\n tableName = parts[1];\n } else {\n schemaName = 'public';\n }\n\n return {\n List: async () => {\n try {\n const response = await this.client!.get(\n `${this.options.base_url}/${this.database}/${schemaName}/${tableName}`,\n );\n return await response.json();\n } catch (error: any) {\n throw new Error(\n `Failed to fetch data from ${tableName}: ${error.message}`,\n );\n }\n },\n Show: async () => {\n try {\n const response = await this.client!.get(\n `${this.options.base_url}/show/${this.database}/${schemaName}/${tableName}`,\n );\n return await response.json();\n } catch (error: any) {\n throw new Error(\n `Failed to show data for ${tableName}: ${error.message}`,\n );\n }\n },\n };\n }\n\n /**\n * Gets the name of the database to which the client is connected.\n */\n get database(): string {\n return this.options.database;\n }\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA;;;;;AAKG;AACH,IAAA,cAAA,kBAAA,YAAA;AAeE;;;;AAIG;AACH,IAAA,SAAA,cAAA,CAAY,OAA8B,EAAA;AACxC,QAAA,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,YAAY,EAAE,CAAC;KACrB;AAED;;AAEG;AACW,IAAA,cAAA,CAAA,SAAA,CAAA,YAAY,GAA1B,YAAA;;;;;AACE,gBAAA,IAAI;AACI,oBAAA,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC;AAClC,oBAAA,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC;oBACjC,YAAa,GAAA,QAAQ,GAAG,IAAI,CAAC,QAAQ,GAAG,GAAG,GAAG,QAAQ,CAAC,CAAC;oBAE9D,IAAI,CAAC,MAAM,GAAG;wBACZ,GAAG,EAAE,UAAO,GAAW,EAAA,EAAA,OAAA,SAAA,CAAA,KAAA,EAAA,KAAA,CAAA,EAAA,KAAA,CAAA,EAAA,YAAA;;;;4CACJ,OAAM,CAAA,CAAA,YAAA,KAAK,CAAC,GAAG,EAAE;AAChC,4CAAA,OAAO,EAAE;AACP,gDAAA,aAAa,EAAE,YAAU;AAC1B,6CAAA;AACF,yCAAA,CAAC,CAAA,CAAA;;AAJI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AA;;;4BAE7B,MAAM,IAAI,KAAK,CACb,0BAA2B,CAAA,MAAA,CAAA,SAAS,EAAK,IAAA,CAAA,CAAA,MAAA,CAAA,OAAK,CAAC,OAAO,CAAE,CACzD,CAAC;;;;AAEL,aAAA,CAAA,CAAA,EAAA;SACF,CAAC;KACH,CAAA;AAKD,IAAA,MAAA,CAAA,cAAA,CAAI,cAAQ,CAAA,SAAA,EAAA,UAAA,EAAA;AAHZ;;AAEG;AACH,QAAA,GAAA,EAAA,YAAA;AACE,YAAA,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC;SAC9B;;;AAAA,KAAA,CAAA,CAAA;IACH,OAAC,cAAA,CAAA;AAAD,CAAC,EAAA;;;;"} \ No newline at end of file diff --git a/dist/index.esm.js b/dist/index.esm.js index e4570a6..1567098 100644 --- a/dist/index.esm.js +++ b/dist/index.esm.js @@ -1,5 +1,5 @@ /*! - * prest-client v0.0.4 + * prest-client v0.0.5 * (c) pgEdge * Released under the MIT License. */ @@ -162,6 +162,24 @@ var PrestApiClient = /** @class */ (function () { } }); }); }, + Show: function () { return __awaiter(_this, void 0, void 0, function () { + var response, error_2; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + _a.trys.push([0, 3, , 4]); + return [4 /*yield*/, this.client.get("".concat(this.options.base_url, "/show/").concat(this.database, "/").concat(schemaName, "/").concat(tableName))]; + case 1: + response = _a.sent(); + return [4 /*yield*/, response.json()]; + case 2: return [2 /*return*/, _a.sent()]; + case 3: + error_2 = _a.sent(); + throw new Error("Failed to show data for ".concat(tableName, ": ").concat(error_2.message)); + case 4: return [2 /*return*/]; + } + }); + }); }, }; }; Object.defineProperty(PrestApiClient.prototype, "database", { diff --git a/dist/index.esm.js.map b/dist/index.esm.js.map index 596bd55..809ad76 100644 --- a/dist/index.esm.js.map +++ b/dist/index.esm.js.map @@ -1 +1 @@ -{"version":3,"file":"index.esm.js","sources":["../src/api/client.ts"],"sourcesContent":["/**\n * Options for creating a Prest API client.\n *\n * @export\n * @interface PrestApiClientOptions\n */\nexport interface PrestApiClientOptions {\n /**\n * The base URL of the Prest API endpoint.\n */\n base_url: string;\n\n /**\n * The username for authentication with the Prest API.\n */\n user_name: string;\n\n /**\n * The password for authentication with the Prest API.\n */\n password: string;\n\n /**\n * The name of the database to connect to.\n */\n database: string;\n}\n\n/**\n * A client for interacting with a Prest API.\n *\n * @export\n * @class PrestApiClient\n */\nexport class PrestApiClient {\n /**\n * The underlying HTTP client for making requests to the Prest API.\n */\n private client:\n | undefined\n | {\n get: (url: string) => Promise;\n };\n\n /**\n * The options used to configure the client.\n */\n private options: PrestApiClientOptions;\n\n /**\n * Creates a new Prest API client with the provided options.\n *\n * @param options - The options for creating the client.\n */\n constructor(options: PrestApiClientOptions) {\n this.options = options;\n this.createClient();\n }\n\n /**\n * Creates the underlying HTTP client with the necessary authentication headers.\n */\n private async createClient() {\n try {\n const username = this.options.user_name;\n const password = this.options.password;\n const authHeader = 'Basic ' + btoa(username + ':' + password);\n\n this.client = {\n get: async (url: string) => {\n const response = await fetch(url, {\n headers: {\n Authorization: authHeader,\n },\n });\n\n if (!response.ok) {\n throw new Error(`Failed to fetch data: ${response.statusText}`);\n }\n\n return response;\n },\n };\n } catch (error) {\n console.error('Error creating client:', error);\n }\n }\n\n /**\n * Returns an object for interacting with a specific table in the database.\n *\n * @param tableName - The name of the table.\n * @returns An object with methods for interacting with the table.\n */\n Table(tableName: string | undefined): { List: () => Promise } {\n if (!this.client) {\n throw new Error('Client not initialized');\n }\n\n if (!tableName) {\n throw new Error('Table name is required');\n }\n\n let schemaName: string | undefined;\n if (tableName.includes('.')) {\n const parts = tableName.split('.');\n schemaName = parts[0];\n tableName = parts[1];\n } else {\n schemaName = 'public';\n }\n\n return {\n List: async () => {\n try {\n const response = await this.client!.get(\n `${this.options.base_url}/${this.database}/${schemaName}/${tableName}`,\n );\n return await response.json();\n } catch (error: any) {\n throw new Error(\n `Failed to fetch data from ${tableName}: ${error.message}`,\n );\n }\n },\n };\n }\n\n /**\n * Gets the name of the database to which the client is connected.\n */\n get database(): string {\n return this.options.database;\n }\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA;;;;;AAKG;AACH,IAAA,cAAA,kBAAA,YAAA;AAeE;;;;AAIG;AACH,IAAA,SAAA,cAAA,CAAY,OAA8B,EAAA;AACxC,QAAA,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,YAAY,EAAE,CAAC;KACrB;AAED;;AAEG;AACW,IAAA,cAAA,CAAA,SAAA,CAAA,YAAY,GAA1B,YAAA;;;;;AACE,gBAAA,IAAI;AACI,oBAAA,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC;AAClC,oBAAA,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC;oBACjC,YAAa,GAAA,QAAQ,GAAG,IAAI,CAAC,QAAQ,GAAG,GAAG,GAAG,QAAQ,CAAC,CAAC;oBAE9D,IAAI,CAAC,MAAM,GAAG;wBACZ,GAAG,EAAE,UAAO,GAAW,EAAA,EAAA,OAAA,SAAA,CAAA,KAAA,EAAA,KAAA,CAAA,EAAA,KAAA,CAAA,EAAA,YAAA;;;;4CACJ,OAAM,CAAA,CAAA,YAAA,KAAK,CAAC,GAAG,EAAE;AAChC,4CAAA,OAAO,EAAE;AACP,gDAAA,aAAa,EAAE,YAAU;AAC1B,6CAAA;AACF,yCAAA,CAAC,CAAA,CAAA;;AAJI,wCAAA,QAAQ,GAAG,EAIf,CAAA,IAAA,EAAA,CAAA;AAEF,wCAAA,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE;4CAChB,MAAM,IAAI,KAAK,CAAC,wBAAA,CAAA,MAAA,CAAyB,QAAQ,CAAC,UAAU,CAAE,CAAC,CAAC;yCACjE;AAED,wCAAA,OAAA,CAAA,CAAA,aAAO,QAAQ,CAAC,CAAA;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QAAQ,GAAG,EAEhB,CAAA,IAAA,EAAA,CAAA;AACM,4BAAA,OAAA,CAAA,CAAA,YAAM,QAAQ,CAAC,IAAI,EAAE,CAAA,CAAA;AAA5B,wBAAA,KAAA,CAAA,EAAA,OAAA,CAAA,CAAA,aAAO,SAAqB,CAAC,CAAA;;;4BAE7B,MAAM,IAAI,KAAK,CACb,4BAA6B,CAAA,MAAA,CAAA,SAAS,EAAK,IAAA,CAAA,CAAA,MAAA,CAAA,OAAK,CAAC,OAAO,CAAE,CAC3D,CAAC;;;;AAEL,aAAA,CAAA,CAAA,EAAA;SACF,CAAC;KACH,CAAA;AAKD,IAAA,MAAA,CAAA,cAAA,CAAI,cAAQ,CAAA,SAAA,EAAA,UAAA,EAAA;AAHZ;;AAEG;AACH,QAAA,GAAA,EAAA,YAAA;AACE,YAAA,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC;SAC9B;;;AAAA,KAAA,CAAA,CAAA;IACH,OAAC,cAAA,CAAA;AAAD,CAAC,EAAA;;;;"} \ No newline at end of file +{"version":3,"file":"index.esm.js","sources":["../src/api/client.ts"],"sourcesContent":["/**\n * Options for creating a Prest API client.\n *\n * @export\n * @interface PrestApiClientOptions\n */\nexport interface PrestApiClientOptions {\n /**\n * The base URL of the Prest API endpoint.\n */\n base_url: string;\n\n /**\n * The username for authentication with the Prest API.\n */\n user_name: string;\n\n /**\n * The password for authentication with the Prest API.\n */\n password: string;\n\n /**\n * The name of the database to connect to.\n */\n database: string;\n}\n\n/**\n * A client for interacting with a Prest API.\n *\n * @export\n * @class PrestApiClient\n */\nexport class PrestApiClient {\n /**\n * The underlying HTTP client for making requests to the Prest API.\n */\n private client:\n | undefined\n | {\n get: (url: string) => Promise;\n };\n\n /**\n * The options used to configure the client.\n */\n private options: PrestApiClientOptions;\n\n /**\n * Creates a new Prest API client with the provided options.\n *\n * @param options - The options for creating the client.\n */\n constructor(options: PrestApiClientOptions) {\n this.options = options;\n this.createClient();\n }\n\n /**\n * Creates the underlying HTTP client with the necessary authentication headers.\n */\n private async createClient() {\n try {\n const username = this.options.user_name;\n const password = this.options.password;\n const authHeader = 'Basic ' + btoa(username + ':' + password);\n\n this.client = {\n get: async (url: string) => {\n const response = await fetch(url, {\n headers: {\n Authorization: authHeader,\n },\n });\n\n if (!response.ok) {\n throw new Error(`Failed to fetch data: ${response.statusText}`);\n }\n\n return response;\n },\n };\n } catch (error) {\n console.error('Error creating client:', error);\n }\n }\n\n /**\n * Returns an object for interacting with a specific table in the database.\n *\n * @param tableName - The name of the table.\n * @returns An object with methods for interacting with the table.\n */\n Table(tableName: string | undefined): {\n /**\n * Retrieves the structure of the specified table.\n *\n * @returns A promise that resolves with the table structure.\n * @throws An error if fetching the table structure fails.\n *\n * @example\n * const response = await client.Table('user').List();\n * // Queries the rows of the 'user' table. Public schema is used by default.\n * // Executes GET `/:database/:schema/:table`.\n *\n * @example\n * const response = await client.Table('private.user').List();\n * // Retrieves the rows of the 'user' table in the 'private' schema.\n * // Executes GET `/:database/:schema/:table`.\n *\n * @example\n * const response = await client.Table('public.').List();\n * // Retrieves a list of tables in the 'public' schema.\n * // Executes GET `/:database/:schema`.\n * // Note: The dot at the end is to ignore the table name.\n */\n List: () => Promise;\n\n /**\n * Retrieves data from the specified table.\n *\n * @returns A promise that resolves with the data from the table.\n * @throws An error if fetching data from the table fails.\n *\n * @example\n * const response = await client.Table('user').Show();\n * // Retrieves data from the 'categories' table.\n * // Executes GET `/show/:database/:schema/:table`.\n */\n Show: () => Promise;\n } {\n if (!this.client) {\n throw new Error('Client not initialized');\n }\n\n if (!tableName) {\n throw new Error('Table name is required');\n }\n\n let schemaName: string | undefined;\n if (tableName.includes('.')) {\n const parts = tableName.split('.');\n schemaName = parts[0];\n tableName = parts[1];\n } else {\n schemaName = 'public';\n }\n\n return {\n List: async () => {\n try {\n const response = await this.client!.get(\n `${this.options.base_url}/${this.database}/${schemaName}/${tableName}`,\n );\n return await response.json();\n } catch (error: any) {\n throw new Error(\n `Failed to fetch data from ${tableName}: ${error.message}`,\n );\n }\n },\n Show: async () => {\n try {\n const response = await this.client!.get(\n `${this.options.base_url}/show/${this.database}/${schemaName}/${tableName}`,\n );\n return await response.json();\n } catch (error: any) {\n throw new Error(\n `Failed to show data for ${tableName}: ${error.message}`,\n );\n }\n },\n };\n }\n\n /**\n * Gets the name of the database to which the client is connected.\n */\n get database(): string {\n return this.options.database;\n }\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA;;;;;AAKG;AACH,IAAA,cAAA,kBAAA,YAAA;AAeE;;;;AAIG;AACH,IAAA,SAAA,cAAA,CAAY,OAA8B,EAAA;AACxC,QAAA,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,YAAY,EAAE,CAAC;KACrB;AAED;;AAEG;AACW,IAAA,cAAA,CAAA,SAAA,CAAA,YAAY,GAA1B,YAAA;;;;;AACE,gBAAA,IAAI;AACI,oBAAA,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC;AAClC,oBAAA,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC;oBACjC,YAAa,GAAA,QAAQ,GAAG,IAAI,CAAC,QAAQ,GAAG,GAAG,GAAG,QAAQ,CAAC,CAAC;oBAE9D,IAAI,CAAC,MAAM,GAAG;wBACZ,GAAG,EAAE,UAAO,GAAW,EAAA,EAAA,OAAA,SAAA,CAAA,KAAA,EAAA,KAAA,CAAA,EAAA,KAAA,CAAA,EAAA,YAAA;;;;4CACJ,OAAM,CAAA,CAAA,YAAA,KAAK,CAAC,GAAG,EAAE;AAChC,4CAAA,OAAO,EAAE;AACP,gDAAA,aAAa,EAAE,YAAU;AAC1B,6CAAA;AACF,yCAAA,CAAC,CAAA,CAAA;;AAJI,wCAAA,QAAQ,GAAG,EAIf,CAAA,IAAA,EAAA,CAAA;AAEF,wCAAA,IAA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,EAAK,IAAA,CAAA,CAAA,MAAA,CAAA,OAAK,CAAC,OAAO,CAAE,CACzD,CAAC;;;;AAEL,aAAA,CAAA,CAAA,EAAA;SACF,CAAC;KACH,CAAA;AAKD,IAAA,MAAA,CAAA,cAAA,CAAI,cAAQ,CAAA,SAAA,EAAA,UAAA,EAAA;AAHZ;;AAEG;AACH,QAAA,GAAA,EAAA,YAAA;AACE,YAAA,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC;SAC9B;;;AAAA,KAAA,CAAA,CAAA;IACH,OAAC,cAAA,CAAA;AAAD,CAAC,EAAA;;;;"} \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index d987ce8..3587a4b 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,5 +1,5 @@ /*! - * prest-client v0.0.4 + * prest-client v0.0.5 * (c) pgEdge * Released under the MIT License. */ @@ -164,6 +164,24 @@ var PrestApiClient = /** @class */ (function () { } }); }); }, + Show: function () { return __awaiter(_this, void 0, void 0, function () { + var response, error_2; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + _a.trys.push([0, 3, , 4]); + return [4 /*yield*/, this.client.get("".concat(this.options.base_url, "/show/").concat(this.database, "/").concat(schemaName, "/").concat(tableName))]; + case 1: + response = _a.sent(); + return [4 /*yield*/, response.json()]; + case 2: return [2 /*return*/, _a.sent()]; + case 3: + error_2 = _a.sent(); + throw new Error("Failed to show data for ".concat(tableName, ": ").concat(error_2.message)); + case 4: return [2 /*return*/]; + } + }); + }); }, }; }; Object.defineProperty(PrestApiClient.prototype, "database", { diff --git a/dist/index.js.map b/dist/index.js.map index d203aee..fe730f5 100644 --- a/dist/index.js.map +++ b/dist/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sources":["../src/api/client.ts"],"sourcesContent":["/**\n * Options for creating a Prest API client.\n *\n * @export\n * @interface PrestApiClientOptions\n */\nexport interface PrestApiClientOptions {\n /**\n * The base URL of the Prest API endpoint.\n */\n base_url: string;\n\n /**\n * The username for authentication with the Prest API.\n */\n user_name: string;\n\n /**\n * The password for authentication with the Prest API.\n */\n password: string;\n\n /**\n * The name of the database to connect to.\n */\n database: string;\n}\n\n/**\n * A client for interacting with a Prest API.\n *\n * @export\n * @class PrestApiClient\n */\nexport class PrestApiClient {\n /**\n * The underlying HTTP client for making requests to the Prest API.\n */\n private client:\n | undefined\n | {\n get: (url: string) => Promise;\n };\n\n /**\n * The options used to configure the client.\n */\n private options: PrestApiClientOptions;\n\n /**\n * Creates a new Prest API client with the provided options.\n *\n * @param options - The options for creating the client.\n */\n constructor(options: PrestApiClientOptions) {\n this.options = options;\n this.createClient();\n }\n\n /**\n * Creates the underlying HTTP client with the necessary authentication headers.\n */\n private async createClient() {\n try {\n const username = this.options.user_name;\n const password = this.options.password;\n const authHeader = 'Basic ' + btoa(username + ':' + password);\n\n this.client = {\n get: async (url: string) => {\n const response = await fetch(url, {\n headers: {\n Authorization: authHeader,\n },\n });\n\n if (!response.ok) {\n throw new Error(`Failed to fetch data: ${response.statusText}`);\n }\n\n return response;\n },\n };\n } catch (error) {\n console.error('Error creating client:', error);\n }\n }\n\n /**\n * Returns an object for interacting with a specific table in the database.\n *\n * @param tableName - The name of the table.\n * @returns An object with methods for interacting with the table.\n */\n Table(tableName: string | undefined): { List: () => Promise } {\n if (!this.client) {\n throw new Error('Client not initialized');\n }\n\n if (!tableName) {\n throw new Error('Table name is required');\n }\n\n let schemaName: string | undefined;\n if (tableName.includes('.')) {\n const parts = tableName.split('.');\n schemaName = parts[0];\n tableName = parts[1];\n } else {\n schemaName = 'public';\n }\n\n return {\n List: async () => {\n try {\n const response = await this.client!.get(\n `${this.options.base_url}/${this.database}/${schemaName}/${tableName}`,\n );\n return await response.json();\n } catch (error: any) {\n throw new Error(\n `Failed to fetch data from ${tableName}: ${error.message}`,\n );\n }\n },\n };\n }\n\n /**\n * Gets the name of the database to which the client is connected.\n */\n get database(): string {\n return this.options.database;\n }\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;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AI,EAAE,CAAA,CAAA;AAA5B,wBAAA,KAAA,CAAA,EAAA,OAAA,CAAA,CAAA,aAAO,SAAqB,CAAC,CAAA;;;4BAE7B,MAAM,IAAI,KAAK,CACb,4BAA6B,CAAA,MAAA,CAAA,SAAS,EAAK,IAAA,CAAA,CAAA,MAAA,CAAA,OAAK,CAAC,OAAO,CAAE,CAC3D,CAAC;;;;AAEL,aAAA,CAAA,CAAA,EAAA;SACF,CAAC;KACH,CAAA;AAKD,IAAA,MAAA,CAAA,cAAA,CAAI,cAAQ,CAAA,SAAA,EAAA,UAAA,EAAA;AAHZ;;AAEG;AACH,QAAA,GAAA,EAAA,YAAA;AACE,YAAA,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC;SAC9B;;;AAAA,KAAA,CAAA,CAAA;IACH,OAAC,cAAA,CAAA;AAAD,CAAC,EAAA;;;;"} \ No newline at end of file +{"version":3,"file":"index.js","sources":["../src/api/client.ts"],"sourcesContent":["/**\n * Options for creating a Prest API client.\n *\n * @export\n * @interface PrestApiClientOptions\n */\nexport interface PrestApiClientOptions {\n /**\n * The base URL of the Prest API endpoint.\n */\n base_url: string;\n\n /**\n * The username for authentication with the Prest API.\n */\n user_name: string;\n\n /**\n * The password for authentication with the Prest API.\n */\n password: string;\n\n /**\n * The name of the database to connect to.\n */\n database: string;\n}\n\n/**\n * A client for interacting with a Prest API.\n *\n * @export\n * @class PrestApiClient\n */\nexport class PrestApiClient {\n /**\n * The underlying HTTP client for making requests to the Prest API.\n */\n private client:\n | undefined\n | {\n get: (url: string) => Promise;\n };\n\n /**\n * The options used to configure the client.\n */\n private options: PrestApiClientOptions;\n\n /**\n * Creates a new Prest API client with the provided options.\n *\n * @param options - The options for creating the client.\n */\n constructor(options: PrestApiClientOptions) {\n this.options = options;\n this.createClient();\n }\n\n /**\n * Creates the underlying HTTP client with the necessary authentication headers.\n */\n private async createClient() {\n try {\n const username = this.options.user_name;\n const password = this.options.password;\n const authHeader = 'Basic ' + btoa(username + ':' + password);\n\n this.client = {\n get: async (url: string) => {\n const response = await fetch(url, {\n headers: {\n Authorization: authHeader,\n },\n });\n\n if (!response.ok) {\n throw new Error(`Failed to fetch data: ${response.statusText}`);\n }\n\n return response;\n },\n };\n } catch (error) {\n console.error('Error creating client:', error);\n }\n }\n\n /**\n * Returns an object for interacting with a specific table in the database.\n *\n * @param tableName - The name of the table.\n * @returns An object with methods for interacting with the table.\n */\n Table(tableName: string | undefined): {\n /**\n * Retrieves the structure of the specified table.\n *\n * @returns A promise that resolves with the table structure.\n * @throws An error if fetching the table structure fails.\n *\n * @example\n * const response = await client.Table('user').List();\n * // Queries the rows of the 'user' table. Public schema is used by default.\n * // Executes GET `/:database/:schema/:table`.\n *\n * @example\n * const response = await client.Table('private.user').List();\n * // Retrieves the rows of the 'user' table in the 'private' schema.\n * // Executes GET `/:database/:schema/:table`.\n *\n * @example\n * const response = await client.Table('public.').List();\n * // Retrieves a list of tables in the 'public' schema.\n * // Executes GET `/:database/:schema`.\n * // Note: The dot at the end is to ignore the table name.\n */\n List: () => Promise;\n\n /**\n * Retrieves data from the specified table.\n *\n * @returns A promise that resolves with the data from the table.\n * @throws An error if fetching data from the table fails.\n *\n * @example\n * const response = await client.Table('user').Show();\n * // Retrieves data from the 'categories' table.\n * // Executes GET `/show/:database/:schema/:table`.\n */\n Show: () => Promise;\n } {\n if (!this.client) {\n throw new Error('Client not initialized');\n }\n\n if (!tableName) {\n throw new Error('Table name is required');\n }\n\n let schemaName: string | undefined;\n if (tableName.includes('.')) {\n const parts = tableName.split('.');\n schemaName = parts[0];\n tableName = parts[1];\n } else {\n schemaName = 'public';\n }\n\n return {\n List: async () => {\n try {\n const response = await this.client!.get(\n `${this.options.base_url}/${this.database}/${schemaName}/${tableName}`,\n );\n return await response.json();\n } catch (error: any) {\n throw new Error(\n `Failed to fetch data from ${tableName}: ${error.message}`,\n );\n }\n },\n Show: async () => {\n try {\n const response = await this.client!.get(\n `${this.options.base_url}/show/${this.database}/${schemaName}/${tableName}`,\n );\n return await response.json();\n } catch (error: any) {\n throw new Error(\n `Failed to show data for ${tableName}: ${error.message}`,\n );\n }\n },\n };\n }\n\n /**\n * Gets the name of the database to which the client is connected.\n */\n get database(): string {\n return this.options.database;\n }\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA;;;;;AAKG;AACH,IAAA,cAAA,kBAAA,YAAA;AAeE;;;;AAIG;AACH,IAAA,SAAA,cAAA,CAAY,OAA8B,EAAA;AACxC,QAAA,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,YAAY,EAAE,CAAC;KACrB;AAED;;AAEG;AACW,IAAA,cAAA,CAAA,SAAA,CAAA,YAAY,GAA1B,YAAA;;;;;AACE,gBAAA,IAAI;AACI,oBAAA,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC;AAClC,oBAAA,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC;oBACjC,YAAa,GAAA,QAAQ,GAAG,IAAI,CAAC,QAAQ,GAAG,GAAG,GAAG,QAAQ,CAAC,CAAC;oBAE9D,IAAI,CAAC,MAAM,GAAG;wBACZ,GAAG,EAAE,UAAO,GAAW,EAAA,EAAA,OAAA,SAAA,CAAA,KAAA,EAAA,KAAA,CAAA,EAAA,KAAA,CAAA,EAAA,YAAA;;;;4CACJ,OAAM,CAAA,CAAA,YAAA,KAAK,CAAC,GAAG,EAAE;AAChC,4CAAA,OAAO,EAAE;AACP,gDAAA,aAAa,EAAE,YAAU;AAC1B,6CAAA;AACF,yCAAA,CAAC,CAAA,CAAA;;AAJI,wCAAA,QAAQ,GAAG,EAIf,CAAA,IAAA,EAAA,CAAA;AAEF,wCAAA,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE;4CAChB,MAAM,IAAI,KAAK,CAAC,wBAAA,CAAA,MAAA,CAAyB,QAAQ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AA,EAAA;SACF,CAAC;KACH,CAAA;AAKD,IAAA,MAAA,CAAA,cAAA,CAAI,cAAQ,CAAA,SAAA,EAAA,UAAA,EAAA;AAHZ;;AAEG;AACH,QAAA,GAAA,EAAA,YAAA;AACE,YAAA,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC;SAC9B;;;AAAA,KAAA,CAAA,CAAA;IACH,OAAC,cAAA,CAAA;AAAD,CAAC,EAAA;;;;"} \ No newline at end of file diff --git a/dist/index.mjs b/dist/index.mjs index 5958b37..f348198 100644 --- a/dist/index.mjs +++ b/dist/index.mjs @@ -1,5 +1,5 @@ /*! - * prest-client v0.0.4 + * prest-client v0.0.5 * (c) pgEdge * Released under the MIT License. */ @@ -162,6 +162,24 @@ var PrestApiClient = /** @class */ (function () { } }); }); }, + Show: function () { return __awaiter(_this, void 0, void 0, function () { + var response, error_2; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + _a.trys.push([0, 3, , 4]); + return [4 /*yield*/, this.client.get("".concat(this.options.base_url, "/show/").concat(this.database, "/").concat(schemaName, "/").concat(tableName))]; + case 1: + response = _a.sent(); + return [4 /*yield*/, response.json()]; + case 2: return [2 /*return*/, _a.sent()]; + case 3: + error_2 = _a.sent(); + throw new Error("Failed to show data for ".concat(tableName, ": ").concat(error_2.message)); + case 4: return [2 /*return*/]; + } + }); + }); }, }; }; Object.defineProperty(PrestApiClient.prototype, "database", { diff --git a/dist/index.mjs.map b/dist/index.mjs.map index ed3838c..8382f5c 100644 --- a/dist/index.mjs.map +++ b/dist/index.mjs.map @@ -1 +1 @@ -{"version":3,"file":"index.mjs","sources":["../src/api/client.ts"],"sourcesContent":["/**\n * Options for creating a Prest API client.\n *\n * @export\n * @interface PrestApiClientOptions\n */\nexport interface PrestApiClientOptions {\n /**\n * The base URL of the Prest API endpoint.\n */\n base_url: string;\n\n /**\n * The username for authentication with the Prest API.\n */\n user_name: string;\n\n /**\n * The password for authentication with the Prest API.\n */\n password: string;\n\n /**\n * The name of the database to connect to.\n */\n database: string;\n}\n\n/**\n * A client for interacting with a Prest API.\n *\n * @export\n * @class PrestApiClient\n */\nexport class PrestApiClient {\n /**\n * The underlying HTTP client for making requests to the Prest API.\n */\n private client:\n | undefined\n | {\n get: (url: string) => Promise;\n };\n\n /**\n * The options used to configure the client.\n */\n private options: PrestApiClientOptions;\n\n /**\n * Creates a new Prest API client with the provided options.\n *\n * @param options - The options for creating the client.\n */\n constructor(options: PrestApiClientOptions) {\n this.options = options;\n this.createClient();\n }\n\n /**\n * Creates the underlying HTTP client with the necessary authentication headers.\n */\n private async createClient() {\n try {\n const username = this.options.user_name;\n const password = this.options.password;\n const authHeader = 'Basic ' + btoa(username + ':' + password);\n\n this.client = {\n get: async (url: string) => {\n const response = await fetch(url, {\n headers: {\n Authorization: authHeader,\n },\n });\n\n if (!response.ok) {\n throw new Error(`Failed to fetch data: ${response.statusText}`);\n }\n\n return response;\n },\n };\n } catch (error) {\n console.error('Error creating client:', error);\n }\n }\n\n /**\n * Returns an object for interacting with a specific table in the database.\n *\n * @param tableName - The name of the table.\n * @returns An object with methods for interacting with the table.\n */\n Table(tableName: string | undefined): { List: () => Promise } {\n if (!this.client) {\n throw new Error('Client not initialized');\n }\n\n if (!tableName) {\n throw new Error('Table name is required');\n }\n\n let schemaName: string | undefined;\n if (tableName.includes('.')) {\n const parts = tableName.split('.');\n schemaName = parts[0];\n tableName = parts[1];\n } else {\n schemaName = 'public';\n }\n\n return {\n List: async () => {\n try {\n const response = await this.client!.get(\n `${this.options.base_url}/${this.database}/${schemaName}/${tableName}`,\n );\n return await response.json();\n } catch (error: any) {\n throw new Error(\n `Failed to fetch data from ${tableName}: ${error.message}`,\n );\n }\n },\n };\n }\n\n /**\n * Gets the name of the database to which the client is connected.\n */\n get database(): string {\n return this.options.database;\n }\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA;;;;;AAK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AAqB,CAAC,CAAA;;;4BAE7B,MAAM,IAAI,KAAK,CACb,4BAA6B,CAAA,MAAA,CAAA,SAAS,EAAK,IAAA,CAAA,CAAA,MAAA,CAAA,OAAK,CAAC,OAAO,CAAE,CAC3D,CAAC;;;;AAEL,aAAA,CAAA,CAAA,EAAA;SACF,CAAC;KACH,CAAA;AAKD,IAAA,MAAA,CAAA,cAAA,CAAI,cAAQ,CAAA,SAAA,EAAA,UAAA,EAAA;AAHZ;;AAEG;AACH,QAAA,GAAA,EAAA,YAAA;AACE,YAAA,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC;SAC9B;;;AAAA,KAAA,CAAA,CAAA;IACH,OAAC,cAAA,CAAA;AAAD,CAAC,EAAA;;;;"} \ No newline at end of file +{"version":3,"file":"index.mjs","sources":["../src/api/client.ts"],"sourcesContent":["/**\n * Options for creating a Prest API client.\n *\n * @export\n * @interface PrestApiClientOptions\n */\nexport interface PrestApiClientOptions {\n /**\n * The base URL of the Prest API endpoint.\n */\n base_url: string;\n\n /**\n * The username for authentication with the Prest API.\n */\n user_name: string;\n\n /**\n * The password for authentication with the Prest API.\n */\n password: string;\n\n /**\n * The name of the database to connect to.\n */\n database: string;\n}\n\n/**\n * A client for interacting with a Prest API.\n *\n * @export\n * @class PrestApiClient\n */\nexport class PrestApiClient {\n /**\n * The underlying HTTP client for making requests to the Prest API.\n */\n private client:\n | undefined\n | {\n get: (url: string) => Promise;\n };\n\n /**\n * The options used to configure the client.\n */\n private options: PrestApiClientOptions;\n\n /**\n * Creates a new Prest API client with the provided options.\n *\n * @param options - The options for creating the client.\n */\n constructor(options: PrestApiClientOptions) {\n this.options = options;\n this.createClient();\n }\n\n /**\n * Creates the underlying HTTP client with the necessary authentication headers.\n */\n private async createClient() {\n try {\n const username = this.options.user_name;\n const password = this.options.password;\n const authHeader = 'Basic ' + btoa(username + ':' + password);\n\n this.client = {\n get: async (url: string) => {\n const response = await fetch(url, {\n headers: {\n Authorization: authHeader,\n },\n });\n\n if (!response.ok) {\n throw new Error(`Failed to fetch data: ${response.statusText}`);\n }\n\n return response;\n },\n };\n } catch (error) {\n console.error('Error creating client:', error);\n }\n }\n\n /**\n * Returns an object for interacting with a specific table in the database.\n *\n * @param tableName - The name of the table.\n * @returns An object with methods for interacting with the table.\n */\n Table(tableName: string | undefined): {\n /**\n * Retrieves the structure of the specified table.\n *\n * @returns A promise that resolves with the table structure.\n * @throws An error if fetching the table structure fails.\n *\n * @example\n * const response = await client.Table('user').List();\n * // Queries the rows of the 'user' table. Public schema is used by default.\n * // Executes GET `/:database/:schema/:table`.\n *\n * @example\n * const response = await client.Table('private.user').List();\n * // Retrieves the rows of the 'user' table in the 'private' schema.\n * // Executes GET `/:database/:schema/:table`.\n *\n * @example\n * const response = await client.Table('public.').List();\n * // Retrieves a list of tables in the 'public' schema.\n * // Executes GET `/:database/:schema`.\n * // Note: The dot at the end is to ignore the table name.\n */\n List: () => Promise;\n\n /**\n * Retrieves data from the specified table.\n *\n * @returns A promise that resolves with the data from the table.\n * @throws An error if fetching data from the table fails.\n *\n * @example\n * const response = await client.Table('user').Show();\n * // Retrieves data from the 'categories' table.\n * // Executes GET `/show/:database/:schema/:table`.\n */\n Show: () => Promise;\n } {\n if (!this.client) {\n throw new Error('Client not initialized');\n }\n\n if (!tableName) {\n throw new Error('Table name is required');\n }\n\n let schemaName: string | undefined;\n if (tableName.includes('.')) {\n const parts = tableName.split('.');\n schemaName = parts[0];\n tableName = parts[1];\n } else {\n schemaName = 'public';\n }\n\n return {\n List: async () => {\n try {\n const response = await this.client!.get(\n `${this.options.base_url}/${this.database}/${schemaName}/${tableName}`,\n );\n return await response.json();\n } catch (error: any) {\n throw new Error(\n `Failed to fetch data from ${tableName}: ${error.message}`,\n );\n }\n },\n Show: async () => {\n try {\n const response = await this.client!.get(\n `${this.options.base_url}/show/${this.database}/${schemaName}/${tableName}`,\n );\n return await response.json();\n } catch (error: any) {\n throw new Error(\n `Failed to show data for ${tableName}: ${error.message}`,\n );\n }\n },\n };\n }\n\n /**\n * Gets the name of the database to which the client is connected.\n */\n get database(): string {\n return this.options.database;\n }\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA;;;;;AAKG;AACH,IAAA,cAAA,kBAAA,YAAA;AAeE;;;;AAIG;AACH,IAAA,SAAA,cAAA,CAAY,OAA8B,EAAA;AACxC,QAAA,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,YAAY,EAAE,CAAC;KACrB;AAED;;AAEG;AACW,IAAA,cAAA,CAAA,SAAA,CAAA,YAAY,GAA1B,YAAA;;;;;AACE,gBAAA,IAAI;AACI,oBAAA,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC;AAClC,oBAAA,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC;oBACjC,YAAa,GAAA,QAAQ,GAAG,IAAI,CAAC,QAAQ,GAAG,GAAG,GAAG,QAAQ,CAAC,CAAC;oBAE9D,IAAI,CAAC,MAAM,GAAG;wBACZ,GAAG,EAAE,UAAO,GAAW,EAAA,EAAA,OAAA,SAAA,CAAA,KAAA,EAAA,KAAA,CAAA,EAAA,KAAA,CAAA,EAAA,YAAA;;;;4CACJ,OAAM,CAAA,CAAA,YAAA,KAAK,CAAC,GAAG,EAAE;AAChC,4CAAA,OAAO,EAAE;AACP,gDAAA,aAAa,EAAE,YAAU;AAC1B,6CAAA;AACF,yCAAA,CAAC,CAAA,CAAA;;AAJI,wCAAA,QAAQ,GAAG,EAIf,CAAA,IAAA,EAAA,CAAA;AAEF,wCAAA,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE;4CAChB,MAAM,IAAI,KAAK,CAAC,wBAAA,CAAA,MAAA,CAAyB,QAAQ,CAAC,UAAU,CAAE,CAAC,CAAC;yCACjE;AAED,wCAAA,OAAA,CAAA,CAAA,aAAO,QAAQ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,SAAA,EAAA,UAAA,EAAA;AAHZ;;AAEG;AACH,QAAA,GAAA,EAAA,YAAA;AACE,YAAA,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC;SAC9B;;;AAAA,KAAA,CAAA,CAAA;IACH,OAAC,cAAA,CAAA;AAAD,CAAC,EAAA;;;;"} \ No newline at end of file diff --git a/dist/index.umd.js b/dist/index.umd.js index efdb22a..bd4c89e 100644 --- a/dist/index.umd.js +++ b/dist/index.umd.js @@ -1,5 +1,5 @@ /*! - * prest-client v0.0.4 + * prest-client v0.0.5 * (c) pgEdge * Released under the MIT License. */ @@ -168,6 +168,24 @@ } }); }); }, + Show: function () { return __awaiter(_this, void 0, void 0, function () { + var response, error_2; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + _a.trys.push([0, 3, , 4]); + return [4 /*yield*/, this.client.get("".concat(this.options.base_url, "/show/").concat(this.database, "/").concat(schemaName, "/").concat(tableName))]; + case 1: + response = _a.sent(); + return [4 /*yield*/, response.json()]; + case 2: return [2 /*return*/, _a.sent()]; + case 3: + error_2 = _a.sent(); + throw new Error("Failed to show data for ".concat(tableName, ": ").concat(error_2.message)); + case 4: return [2 /*return*/]; + } + }); + }); }, }; }; Object.defineProperty(PrestApiClient.prototype, "database", { diff --git a/dist/index.umd.js.map b/dist/index.umd.js.map index a8c3b10..af25241 100644 --- a/dist/index.umd.js.map +++ b/dist/index.umd.js.map @@ -1 +1 @@ -{"version":3,"file":"index.umd.js","sources":["../src/api/client.ts"],"sourcesContent":["/**\n * Options for creating a Prest API client.\n *\n * @export\n * @interface PrestApiClientOptions\n */\nexport interface PrestApiClientOptions {\n /**\n * The base URL of the Prest API endpoint.\n */\n base_url: string;\n\n /**\n * The username for authentication with the Prest API.\n */\n user_name: string;\n\n /**\n * The password for authentication with the Prest API.\n */\n password: string;\n\n /**\n * The name of the database to connect to.\n */\n database: string;\n}\n\n/**\n * A client for interacting with a Prest API.\n *\n * @export\n * @class PrestApiClient\n */\nexport class PrestApiClient {\n /**\n * The underlying HTTP client for making requests to the Prest API.\n */\n private client:\n | undefined\n | {\n get: (url: string) => Promise;\n };\n\n /**\n * The options used to configure the client.\n */\n private options: PrestApiClientOptions;\n\n /**\n * Creates a new Prest API client with the provided options.\n *\n * @param options - The options for creating the client.\n */\n constructor(options: PrestApiClientOptions) {\n this.options = options;\n this.createClient();\n }\n\n /**\n * Creates the underlying HTTP client with the necessary authentication headers.\n */\n private async createClient() {\n try {\n const username = this.options.user_name;\n const password = this.options.password;\n const authHeader = 'Basic ' + btoa(username + ':' + password);\n\n this.client = {\n get: async (url: string) => {\n const response = await fetch(url, {\n headers: {\n Authorization: authHeader,\n },\n });\n\n if (!response.ok) {\n throw new Error(`Failed to fetch data: ${response.statusText}`);\n }\n\n return response;\n },\n };\n } catch (error) {\n console.error('Error creating client:', error);\n }\n }\n\n /**\n * Returns an object for interacting with a specific table in the database.\n *\n * @param tableName - The name of the table.\n * @returns An object with methods for interacting with the table.\n */\n Table(tableName: string | undefined): { List: () => Promise } {\n if (!this.client) {\n throw new Error('Client not initialized');\n }\n\n if (!tableName) {\n throw new Error('Table name is required');\n }\n\n let schemaName: string | undefined;\n if (tableName.includes('.')) {\n const parts = tableName.split('.');\n schemaName = parts[0];\n tableName = parts[1];\n } else {\n schemaName = 'public';\n }\n\n return {\n List: async () => {\n try {\n const response = await this.client!.get(\n `${this.options.base_url}/${this.database}/${schemaName}/${tableName}`,\n );\n return await response.json();\n } catch (error: any) {\n throw new Error(\n `Failed to fetch data from ${tableName}: ${error.message}`,\n );\n }\n },\n };\n }\n\n /**\n * Gets the name of the database to which the client is connected.\n */\n get database(): string {\n return this.options.database;\n }\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IA4BA;;;;;IAKG;AACH,QAAA,cAAA,kBAAA,YAAA;IAeE;;;;IAIG;IACH,IAAA,SAAA,cAAA,CAAY,OAA8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SAAS,EAAK,IAAA,CAAA,CAAA,MAAA,CAAA,OAAK,CAAC,OAAO,CAAE,CAC3D,CAAC;;;;IAEL,aAAA,CAAA,CAAA,EAAA;aACF,CAAC;SACH,CAAA;IAKD,IAAA,MAAA,CAAA,cAAA,CAAI,cAAQ,CAAA,SAAA,EAAA,UAAA,EAAA;IAHZ;;IAEG;IACH,QAAA,GAAA,EAAA,YAAA;IACE,YAAA,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC;aAC9B;;;IAAA,KAAA,CAAA,CAAA;QACH,OAAC,cAAA,CAAA;IAAD,CAAC,EAAA;;;;;;;;"} \ No newline at end of file +{"version":3,"file":"index.umd.js","sources":["../src/api/client.ts"],"sourcesContent":["/**\n * Options for creating a Prest API client.\n *\n * @export\n * @interface PrestApiClientOptions\n */\nexport interface PrestApiClientOptions {\n /**\n * The base URL of the Prest API endpoint.\n */\n base_url: string;\n\n /**\n * The username for authentication with the Prest API.\n */\n user_name: string;\n\n /**\n * The password for authentication with the Prest API.\n */\n password: string;\n\n /**\n * The name of the database to connect to.\n */\n database: string;\n}\n\n/**\n * A client for interacting with a Prest API.\n *\n * @export\n * @class PrestApiClient\n */\nexport class PrestApiClient {\n /**\n * The underlying HTTP client for making requests to the Prest API.\n */\n private client:\n | undefined\n | {\n get: (url: string) => Promise;\n };\n\n /**\n * The options used to configure the client.\n */\n private options: PrestApiClientOptions;\n\n /**\n * Creates a new Prest API client with the provided options.\n *\n * @param options - The options for creating the client.\n */\n constructor(options: PrestApiClientOptions) {\n this.options = options;\n this.createClient();\n }\n\n /**\n * Creates the underlying HTTP client with the necessary authentication headers.\n */\n private async createClient() {\n try {\n const username = this.options.user_name;\n const password = this.options.password;\n const authHeader = 'Basic ' + btoa(username + ':' + password);\n\n this.client = {\n get: async (url: string) => {\n const response = await fetch(url, {\n headers: {\n Authorization: authHeader,\n },\n });\n\n if (!response.ok) {\n throw new Error(`Failed to fetch data: ${response.statusText}`);\n }\n\n return response;\n },\n };\n } catch (error) {\n console.error('Error creating client:', error);\n }\n }\n\n /**\n * Returns an object for interacting with a specific table in the database.\n *\n * @param tableName - The name of the table.\n * @returns An object with methods for interacting with the table.\n */\n Table(tableName: string | undefined): {\n /**\n * Retrieves the structure of the specified table.\n *\n * @returns A promise that resolves with the table structure.\n * @throws An error if fetching the table structure fails.\n *\n * @example\n * const response = await client.Table('user').List();\n * // Queries the rows of the 'user' table. Public schema is used by default.\n * // Executes GET `/:database/:schema/:table`.\n *\n * @example\n * const response = await client.Table('private.user').List();\n * // Retrieves the rows of the 'user' table in the 'private' schema.\n * // Executes GET `/:database/:schema/:table`.\n *\n * @example\n * const response = await client.Table('public.').List();\n * // Retrieves a list of tables in the 'public' schema.\n * // Executes GET `/:database/:schema`.\n * // Note: The dot at the end is to ignore the table name.\n */\n List: () => Promise;\n\n /**\n * Retrieves data from the specified table.\n *\n * @returns A promise that resolves with the data from the table.\n * @throws An error if fetching data from the table fails.\n *\n * @example\n * const response = await client.Table('user').Show();\n * // Retrieves data from the 'categories' table.\n * // Executes GET `/show/:database/:schema/:table`.\n */\n Show: () => Promise;\n } {\n if (!this.client) {\n throw new Error('Client not initialized');\n }\n\n if (!tableName) {\n throw new Error('Table name is required');\n }\n\n let schemaName: string | undefined;\n if (tableName.includes('.')) {\n const parts = tableName.split('.');\n schemaName = parts[0];\n tableName = parts[1];\n } else {\n schemaName = 'public';\n }\n\n return {\n List: async () => {\n try {\n const response = await this.client!.get(\n `${this.options.base_url}/${this.database}/${schemaName}/${tableName}`,\n );\n return await response.json();\n } catch (error: any) {\n throw new Error(\n `Failed to fetch data from ${tableName}: ${error.message}`,\n );\n }\n },\n Show: async () => {\n try {\n const response = await this.client!.get(\n `${this.options.base_url}/show/${this.database}/${schemaName}/${tableName}`,\n );\n return await response.json();\n } catch (error: any) {\n throw new Error(\n `Failed to show data for ${tableName}: ${error.message}`,\n );\n }\n },\n };\n }\n\n /**\n * Gets the name of the database to which the client is connected.\n */\n get database(): string {\n return this.options.database;\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,YAAA;IACE,YAAA,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC;aAC9B;;;IAAA,KAAA,CAAA,CAAA;QACH,OAAC,cAAA,CAAA;IAAD,CAAC,EAAA;;;;;;;;"} \ No newline at end of file diff --git a/dist/index.umd.min.js b/dist/index.umd.min.js index e226e02..3174a6f 100644 --- a/dist/index.umd.min.js +++ b/dist/index.umd.min.js @@ -1,7 +1,7 @@ /*! - * prest-client v0.0.4 + * prest-client v0.0.5 * (c) pgEdge * Released under the MIT License. */ -!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).prestClient={})}(this,(function(t){"use strict";function e(t,e,n,r){return new(n||(n=Promise))((function(o,i){function a(t){try{s(r.next(t))}catch(t){i(t)}}function c(t){try{s(r.throw(t))}catch(t){i(t)}}function s(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(a,c)}s((r=r.apply(t,e||[])).next())}))}function n(t,e){var n,r,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function c(c){return function(s){return function(c){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,c[0]&&(a=0)),a;)try{if(n=1,r&&(o=2&c[0]?r.return:c[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,c[1])).done)return o;switch(r=0,o&&(c=[2&c[0],o.value]),c[0]){case 0:case 1:o=c;break;case 4:return a.label++,{value:c[1],done:!1};case 5:a.label++,r=c[1],c=[0];continue;case 7:c=a.ops.pop(),a.trys.pop();continue;default:if(!(o=a.trys,(o=o.length>0&&o[o.length-1])||6!==c[0]&&2!==c[0])){a=0;continue}if(3===c[0]&&(!o||c[1]>o[0]&&c[1]0&&o[o.length-1])||6!==c[0]&&2!==c[0])){a=0;continue}if(3===c[0]&&(!o||c[1]>o[0]&&c[1] Promise;\n };\n\n /**\n * The options used to configure the client.\n */\n private options: PrestApiClientOptions;\n\n /**\n * Creates a new Prest API client with the provided options.\n *\n * @param options - The options for creating the client.\n */\n constructor(options: PrestApiClientOptions) {\n this.options = options;\n this.createClient();\n }\n\n /**\n * Creates the underlying HTTP client with the necessary authentication headers.\n */\n private async createClient() {\n try {\n const username = this.options.user_name;\n const password = this.options.password;\n const authHeader = 'Basic ' + btoa(username + ':' + password);\n\n this.client = {\n get: async (url: string) => {\n const response = await fetch(url, {\n headers: {\n Authorization: authHeader,\n },\n });\n\n if (!response.ok) {\n throw new Error(`Failed to fetch data: ${response.statusText}`);\n }\n\n return response;\n },\n };\n } catch (error) {\n console.error('Error creating client:', error);\n }\n }\n\n /**\n * Returns an object for interacting with a specific table in the database.\n *\n * @param tableName - The name of the table.\n * @returns An object with methods for interacting with the table.\n */\n Table(tableName: string | undefined): { List: () => Promise } {\n if (!this.client) {\n throw new Error('Client not initialized');\n }\n\n if (!tableName) {\n throw new Error('Table name is required');\n }\n\n let schemaName: string | undefined;\n if (tableName.includes('.')) {\n const parts = tableName.split('.');\n schemaName = parts[0];\n tableName = parts[1];\n } else {\n schemaName = 'public';\n }\n\n return {\n List: async () => {\n try {\n const response = await this.client!.get(\n `${this.options.base_url}/${this.database}/${schemaName}/${tableName}`,\n );\n return await response.json();\n } catch (error: any) {\n throw new Error(\n `Failed to fetch data from ${tableName}: ${error.message}`,\n );\n }\n },\n };\n }\n\n /**\n * Gets the name of the database to which the client is connected.\n */\n get database(): string {\n return this.options.database;\n }\n}\n"],"names":["PrestApiClient","options","this","createClient","prototype","username","user_name","password","authHeader_1","btoa","client","get","url","__awaiter","_this","fetch","headers","Authorization","response","_a","sent","ok","Error","concat","statusText","error","console","Table","tableName","schemaName","includes","parts","split","List","base_url","database","json","error_1","message","Object","defineProperty"],"mappings":";;;;;uoDAkCA,IAAAA,EAAA,WAoBE,SAAAA,EA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,WACE,OAAOT,KAAKD,QAAQkC,QACrB,kCACFnC,CAAD"} \ No newline at end of file +{"version":3,"file":"index.umd.min.js","sources":["../src/api/client.ts"],"sourcesContent":["/**\n * Options for creating a Prest API client.\n *\n * @export\n * @interface PrestApiClientOptions\n */\nexport interface PrestApiClientOptions {\n /**\n * The base URL of the Prest API endpoint.\n */\n base_url: string;\n\n /**\n * The username for authentication with the Prest API.\n */\n user_name: string;\n\n /**\n * The password for authentication with the Prest API.\n */\n password: string;\n\n /**\n * The name of the database to connect to.\n */\n database: string;\n}\n\n/**\n * A client for interacting with a Prest API.\n *\n * @export\n * @class PrestApiClient\n */\nexport class PrestApiClient {\n /**\n * The underlying HTTP client for making requests to the Prest API.\n */\n private client:\n | undefined\n | {\n get: (url: string) => Promise;\n };\n\n /**\n * The options used to configure the client.\n */\n private options: PrestApiClientOptions;\n\n /**\n * Creates a new Prest API client with the provided options.\n *\n * @param options - The options for creating the client.\n */\n constructor(options: PrestApiClientOptions) {\n this.options = options;\n this.createClient();\n }\n\n /**\n * Creates the underlying HTTP client with the necessary authentication headers.\n */\n private async createClient() {\n try {\n const username = this.options.user_name;\n const password = this.options.password;\n const authHeader = 'Basic ' + btoa(username + ':' + password);\n\n this.client = {\n get: async (url: string) => {\n const response = await fetch(url, {\n headers: {\n Authorization: authHeader,\n },\n });\n\n if (!response.ok) {\n throw new Error(`Failed to fetch data: ${response.statusText}`);\n }\n\n return response;\n },\n };\n } catch (error) {\n console.error('Error creating client:', error);\n }\n }\n\n /**\n * Returns an object for interacting with a specific table in the database.\n *\n * @param tableName - The name of the table.\n * @returns An object with methods for interacting with the table.\n */\n Table(tableName: string | undefined): {\n /**\n * Retrieves the structure of the specified table.\n *\n * @returns A promise that resolves with the table structure.\n * @throws An error if fetching the table structure fails.\n *\n * @example\n * const response = await client.Table('user').List();\n * // Queries the rows of the 'user' table. Public schema is used by default.\n * // Executes GET `/:database/:schema/:table`.\n *\n * @example\n * const response = await client.Table('private.user').List();\n * // Retrieves the rows of the 'user' table in the 'private' schema.\n * // Executes GET `/:database/:schema/:table`.\n *\n * @example\n * const response = await client.Table('public.').List();\n * // Retrieves a list of tables in the 'public' schema.\n * // Executes GET `/:database/:schema`.\n * // Note: The dot at the end is to ignore the table name.\n */\n List: () => Promise;\n\n /**\n * Retrieves data from the specified table.\n *\n * @returns A promise that resolves with the data from the table.\n * @throws An error if fetching data from the table fails.\n *\n * @example\n * const response = await client.Table('user').Show();\n * // Retrieves data from the 'categories' table.\n * // Executes GET `/show/:database/:schema/:table`.\n */\n Show: () => Promise;\n } {\n if (!this.client) {\n throw new Error('Client not initialized');\n }\n\n if (!tableName) {\n throw new Error('Table name is required');\n }\n\n let schemaName: string | undefined;\n if (tableName.includes('.')) {\n const parts = tableName.split('.');\n schemaName = parts[0];\n tableName = parts[1];\n } else {\n schemaName = 'public';\n }\n\n return {\n List: async () => {\n try {\n const response = await this.client!.get(\n `${this.options.base_url}/${this.database}/${schemaName}/${tableName}`,\n );\n return await response.json();\n } catch (error: any) {\n throw new Error(\n `Failed to fetch data from ${tableName}: ${error.message}`,\n );\n }\n },\n Show: async () => {\n try {\n const response = await this.client!.get(\n `${this.options.base_url}/show/${this.database}/${schemaName}/${tableName}`,\n );\n return await response.json();\n } catch (error: any) {\n throw new Error(\n `Failed to show data for ${tableName}: ${error.message}`,\n );\n }\n },\n };\n }\n\n /**\n * Gets the name of the database to which the client is connected.\n */\n get database(): string {\n return this.options.database;\n }\n}\n"],"names":["PrestApiClient","options","this","createClient","prototype","username","user_name","password","authHeader_1","btoa","client","get","url","__awaiter","_this","fetch","headers","Authorization","response","_a","sent","ok","Error","concat","statusText","error","console","Table","tableName","schemaName","includes","parts","split","List","base_url","database","json","error_1","message","Show","error_2","Object","defineProperty"],"mappings":";;;;;uoDAkCA,IAAAA,EAAA,WAoBE,SAAAA,EAAYC,GACVC,KAAKD,QAAUA,EACfC,KAAKC,cACN,CA8HH,OAzHgBH,EAAAI,UAAAD,aAAd,+FACE,IACQE,EAAW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AAArB,KAAKiC,SAAQ,KAAAZ,OAAIM,EAAc,KAAAN,OAAAK,YAE3D,MAAA,CAAA,EAHUT,EAEhBC,OACqBgB,QAAtB,KAAA,EAAA,MAAA,CAAA,EAAOjB,iBAEP,iBAAM,IAAIG,MACR,2BAA2BC,OAAAK,EAAc,MAAAL,OAAAiB,EAAMF,8BAGpD,GAAA,IAOLG,OAAAC,eAAI1C,EAAQI,UAAA,WAAA,CAAZO,IAAA,WACE,OAAOT,KAAKD,QAAQkC,QACrB,kCACFnC,CAAD"} \ No newline at end of file diff --git a/docs/classes/PrestApiClient.md b/docs/classes/PrestApiClient.md index 170b202..1ed08e7 100644 --- a/docs/classes/PrestApiClient.md +++ b/docs/classes/PrestApiClient.md @@ -48,7 +48,7 @@ Creates a new Prest API client with the provided options. #### Defined in -[api/client.ts:55](https://github.com/pgEdge/prest-client/blob/269f4a6/src/api/client.ts#L55) +[api/client.ts:55](https://github.com/pgEdge/prest-client/blob/fff97f2/src/api/client.ts#L55) ## Properties @@ -60,7 +60,7 @@ The underlying HTTP client for making requests to the Prest API. #### Defined in -[api/client.ts:39](https://github.com/pgEdge/prest-client/blob/269f4a6/src/api/client.ts#L39) +[api/client.ts:39](https://github.com/pgEdge/prest-client/blob/fff97f2/src/api/client.ts#L39) ___ @@ -72,7 +72,7 @@ The options used to configure the client. #### Defined in -[api/client.ts:48](https://github.com/pgEdge/prest-client/blob/269f4a6/src/api/client.ts#L48) +[api/client.ts:48](https://github.com/pgEdge/prest-client/blob/fff97f2/src/api/client.ts#L48) ## Accessors @@ -88,7 +88,7 @@ Gets the name of the database to which the client is connected. #### Defined in -[api/client.ts:132](https://github.com/pgEdge/prest-client/blob/269f4a6/src/api/client.ts#L132) +[api/client.ts:181](https://github.com/pgEdge/prest-client/blob/fff97f2/src/api/client.ts#L181) ## Methods @@ -110,13 +110,14 @@ Returns an object for interacting with a specific table in the database. An object with methods for interacting with the table. -| Name | Type | -| :------ | :------ | -| `List` | () => `Promise`\<`any`\> | +| Name | Type | Description | +| :------ | :------ | :------ | +| `List` | () => `Promise`\<`any`\> | Retrieves the structure of the specified table. **`Throws`** An error if fetching the table structure fails. **`Example`** ```ts const response = await client.Table('user').List(); // Queries the rows of the 'user' table. Public schema is used by default. // Executes GET `/:database/:schema/:table`. ``` **`Example`** ```ts const response = await client.Table('private.user').List(); // Retrieves the rows of the 'user' table in the 'private' schema. // Executes GET `/:database/:schema/:table`. ``` **`Example`** ```ts const response = await client.Table('public.').List(); // Retrieves a list of tables in the 'public' schema. // Executes GET `/:database/:schema`. // Note: The dot at the end is to ignore the table name. ``` | +| `Show` | () => `Promise`\<`any`\> | Retrieves data from the specified table. **`Throws`** An error if fetching data from the table fails. **`Example`** ```ts const response = await client.Table('user').Show(); // Retrieves data from the 'categories' table. // Executes GET `/show/:database/:schema/:table`. ``` | #### Defined in -[api/client.ts:95](https://github.com/pgEdge/prest-client/blob/269f4a6/src/api/client.ts#L95) +[api/client.ts:95](https://github.com/pgEdge/prest-client/blob/fff97f2/src/api/client.ts#L95) ___ @@ -132,4 +133,4 @@ Creates the underlying HTTP client with the necessary authentication headers. #### Defined in -[api/client.ts:63](https://github.com/pgEdge/prest-client/blob/269f4a6/src/api/client.ts#L63) +[api/client.ts:63](https://github.com/pgEdge/prest-client/blob/fff97f2/src/api/client.ts#L63) diff --git a/docs/interfaces/PrestApiClientOptions.md b/docs/interfaces/PrestApiClientOptions.md index 6e95b20..e28c273 100644 --- a/docs/interfaces/PrestApiClientOptions.md +++ b/docs/interfaces/PrestApiClientOptions.md @@ -27,7 +27,7 @@ The base URL of the Prest API endpoint. #### Defined in -[api/client.ts:11](https://github.com/pgEdge/prest-client/blob/269f4a6/src/api/client.ts#L11) +[api/client.ts:11](https://github.com/pgEdge/prest-client/blob/fff97f2/src/api/client.ts#L11) ___ @@ -39,7 +39,7 @@ The name of the database to connect to. #### Defined in -[api/client.ts:26](https://github.com/pgEdge/prest-client/blob/269f4a6/src/api/client.ts#L26) +[api/client.ts:26](https://github.com/pgEdge/prest-client/blob/fff97f2/src/api/client.ts#L26) ___ @@ -51,7 +51,7 @@ The password for authentication with the Prest API. #### Defined in -[api/client.ts:21](https://github.com/pgEdge/prest-client/blob/269f4a6/src/api/client.ts#L21) +[api/client.ts:21](https://github.com/pgEdge/prest-client/blob/fff97f2/src/api/client.ts#L21) ___ @@ -63,4 +63,4 @@ The username for authentication with the Prest API. #### Defined in -[api/client.ts:16](https://github.com/pgEdge/prest-client/blob/269f4a6/src/api/client.ts#L16) +[api/client.ts:16](https://github.com/pgEdge/prest-client/blob/fff97f2/src/api/client.ts#L16) diff --git a/src/api/client.ts b/src/api/client.ts index 3219228..3bc719d 100644 --- a/src/api/client.ts +++ b/src/api/client.ts @@ -92,43 +92,43 @@ export class PrestApiClient { * @param tableName - The name of the table. * @returns An object with methods for interacting with the table. */ - Table(tableName: string | undefined): { + Table(tableName: string | undefined): { /** - * Retrieves the structure of the specified table. - * - * @returns A promise that resolves with the table structure. - * @throws An error if fetching the table structure fails. - * - * @example - * const response = await client.Table('user').List(); - * // Queries the rows of the 'user' table. Public schema is used by default. - * // Executes GET `/:database/:schema/:table`. - * - * @example - * const response = await client.Table('private.user').List(); - * // Retrieves the rows of the 'user' table in the 'private' schema. - * // Executes GET `/:database/:schema/:table`. - * - * @example - * const response = await client.Table('public.').List(); - * // Retrieves a list of tables in the 'public' schema. - * // Executes GET `/:database/:schema`. - * // Note: The dot at the end is to ignore the table name. - */ - List: () => Promise, - + * Retrieves the structure of the specified table. + * + * @returns A promise that resolves with the table structure. + * @throws An error if fetching the table structure fails. + * + * @example + * const response = await client.Table('user').List(); + * // Queries the rows of the 'user' table. Public schema is used by default. + * // Executes GET `/:database/:schema/:table`. + * + * @example + * const response = await client.Table('private.user').List(); + * // Retrieves the rows of the 'user' table in the 'private' schema. + * // Executes GET `/:database/:schema/:table`. + * + * @example + * const response = await client.Table('public.').List(); + * // Retrieves a list of tables in the 'public' schema. + * // Executes GET `/:database/:schema`. + * // Note: The dot at the end is to ignore the table name. + */ + List: () => Promise; + /** - * Retrieves data from the specified table. - * - * @returns A promise that resolves with the data from the table. - * @throws An error if fetching data from the table fails. - * - * @example - * const response = await client.Table('user').Show(); - * // Retrieves data from the 'categories' table. - * // Executes GET `/show/:database/:schema/:table`. - */ - Show: () => Promise + * Retrieves data from the specified table. + * + * @returns A promise that resolves with the data from the table. + * @throws An error if fetching data from the table fails. + * + * @example + * const response = await client.Table('user').Show(); + * // Retrieves data from the 'categories' table. + * // Executes GET `/show/:database/:schema/:table`. + */ + Show: () => Promise; } { if (!this.client) { throw new Error('Client not initialized'); @@ -171,7 +171,7 @@ export class PrestApiClient { `Failed to show data for ${tableName}: ${error.message}`, ); } - } + }, }; } diff --git a/types/api/client.d.ts b/types/api/client.d.ts index 5d50aa8..90ab1bb 100644 --- a/types/api/client.d.ts +++ b/types/api/client.d.ts @@ -54,7 +54,41 @@ export declare class PrestApiClient { * @returns An object with methods for interacting with the table. */ Table(tableName: string | undefined): { + /** + * Retrieves the structure of the specified table. + * + * @returns A promise that resolves with the table structure. + * @throws An error if fetching the table structure fails. + * + * @example + * const response = await client.Table('user').List(); + * // Queries the rows of the 'user' table. Public schema is used by default. + * // Executes GET `/:database/:schema/:table`. + * + * @example + * const response = await client.Table('private.user').List(); + * // Retrieves the rows of the 'user' table in the 'private' schema. + * // Executes GET `/:database/:schema/:table`. + * + * @example + * const response = await client.Table('public.').List(); + * // Retrieves a list of tables in the 'public' schema. + * // Executes GET `/:database/:schema`. + * // Note: The dot at the end is to ignore the table name. + */ List: () => Promise; + /** + * Retrieves data from the specified table. + * + * @returns A promise that resolves with the data from the table. + * @throws An error if fetching data from the table fails. + * + * @example + * const response = await client.Table('user').Show(); + * // Retrieves data from the 'categories' table. + * // Executes GET `/show/:database/:schema/:table`. + */ + Show: () => Promise; }; /** * Gets the name of the database to which the client is connected. diff --git a/types/api/client.d.ts.map b/types/api/client.d.ts.map index 95b7655..18b6fc6 100644 --- a/types/api/client.d.ts.map +++ b/types/api/client.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/api/client.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,WAAW,qBAAqB;IACpC;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAC;IAEjB;;OAEG;IACH,SAAS,EAAE,MAAM,CAAC;IAElB;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAC;IAEjB;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED;;;;;GAKG;AACH,qBAAa,cAAc;IACzB;;OAEG;IACH,OAAO,CAAC,MAAM,CAIR;IAEN;;OAEG;IACH,OAAO,CAAC,OAAO,CAAwB;IAEvC;;;;OAIG;gBACS,OAAO,EAAE,qBAAqB;IAK1C;;OAEG;YACW,YAAY;IA0B1B;;;;;OAKG;IACH,KAAK,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS,GAAG;QAAE,IAAI,EAAE,MAAM,OAAO,CAAC,GAAG,CAAC,CAAA;KAAE;IAkClE;;OAEG;IACH,IAAI,QAAQ,IAAI,MAAM,CAErB;CACF"} \ No newline at end of file +{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/api/client.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,WAAW,qBAAqB;IACpC;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAC;IAEjB;;OAEG;IACH,SAAS,EAAE,MAAM,CAAC;IAElB;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAC;IAEjB;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED;;;;;GAKG;AACH,qBAAa,cAAc;IACzB;;OAEG;IACH,OAAO,CAAC,MAAM,CAIR;IAEN;;OAEG;IACH,OAAO,CAAC,OAAO,CAAwB;IAEvC;;;;OAIG;gBACS,OAAO,EAAE,qBAAqB;IAK1C;;OAEG;YACW,YAAY;IA0B1B;;;;;OAKG;IACH,KAAK,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS,GAAG;QACpC;;;;;;;;;;;;;;;;;;;;;WAqBG;QACH,IAAI,EAAE,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC;QAEzB;;;;;;;;;;WAUG;QACH,IAAI,EAAE,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC;KAC1B;IA8CD;;OAEG;IACH,IAAI,QAAQ,IAAI,MAAM,CAErB;CACF"} \ No newline at end of file