From fdd72feb105a5527d84aff6cc857d5c7b624a1c9 Mon Sep 17 00:00:00 2001 From: Raymond Tukpe Date: Mon, 28 Oct 2024 14:46:14 +0100 Subject: [PATCH] feat: add typings to queries --- .gitignore | 3 ++- src/Api/common.ts | 6 ++++++ src/Api/portal-link.ts | 12 ++++++------ src/Api/project.ts | 8 ++++---- src/Api/source.ts | 20 ++++++++++---------- src/Api/subscription.ts | 16 ++++++++-------- src/client.ts | 13 ++++++++++++- src/interfaces/portal-links.ts | 6 ++++++ src/interfaces/source.ts | 7 +++++++ src/interfaces/subscription.ts | 6 ++++++ 10 files changed, 67 insertions(+), 30 deletions(-) create mode 100644 src/Api/common.ts diff --git a/.gitignore b/.gitignore index 0771922..f25e8e2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules -/dist \ No newline at end of file +/dist +.idea diff --git a/src/Api/common.ts b/src/Api/common.ts new file mode 100644 index 0000000..cd9fba9 --- /dev/null +++ b/src/Api/common.ts @@ -0,0 +1,6 @@ +export interface Pageable { + perPage?: number; + next_page_cursor?: string; + prev_page_cursor?: string; + direction?: 'next' | 'prev'; +} diff --git a/src/Api/portal-link.ts b/src/Api/portal-link.ts index 02c067a..171000e 100644 --- a/src/Api/portal-link.ts +++ b/src/Api/portal-link.ts @@ -1,6 +1,6 @@ import { Client } from '../client'; import { ResponseHelper } from '../utils/helpers/response-helper'; -import { CreatePortalLink, UpdatePortalLink } from '../interfaces/portal-links'; +import { CreatePortalLink, QueryPortalLinks, UpdatePortalLink } from '../interfaces/portal-links'; export class PortalLink { private client: Client; @@ -9,9 +9,9 @@ export class PortalLink { this.client = client; } - async create(attributes: CreatePortalLink, query?: any) { + async create(attributes: CreatePortalLink) { try { - const { data } = await this.client.httpPost(`/portal-links`, attributes, query); + const { data } = await this.client.httpPost(`/portal-links`, attributes); return data; } catch (error) { ResponseHelper.handleErrors(error); @@ -27,7 +27,7 @@ export class PortalLink { } } - async all(query?: any) { + async all(query?: QueryPortalLinks) { try { const { data } = await this.client.httpGet(`/portal-links`, query); return data; @@ -36,9 +36,9 @@ export class PortalLink { } } - async update(id: string, attributes: UpdatePortalLink, query?: any) { + async update(id: string, attributes: UpdatePortalLink) { try { - const { data } = await this.client.httpPut(`/portal-links/${id}`, attributes, query); + const { data } = await this.client.httpPut(`/portal-links/${id}`, attributes); return data; } catch (error) { ResponseHelper.handleErrors(error); diff --git a/src/Api/project.ts b/src/Api/project.ts index 4023854..8ea502f 100644 --- a/src/Api/project.ts +++ b/src/Api/project.ts @@ -18,18 +18,18 @@ export class Project { } } - async update(attributes: UpdateProject, query?: any) { + async update(attributes: UpdateProject) { try { - const { data } = await this.client.httpPut(`/`, attributes, query); + const { data } = await this.client.httpPut(`/`, attributes); return data; } catch (error) { ResponseHelper.handleErrors(error); } } - async delete(attributes?: any, query?: any) { + async delete(attributes?: any) { try { - const { data } = await this.client.httpDelete(`/`, attributes, query); + const { data } = await this.client.httpDelete(`/`, attributes); return data; } catch (error) { ResponseHelper.handleErrors(error); diff --git a/src/Api/source.ts b/src/Api/source.ts index b8c0b15..062d05d 100644 --- a/src/Api/source.ts +++ b/src/Api/source.ts @@ -1,5 +1,5 @@ import { Client } from '../client'; -import { CreateSource, UpdateSource } from '../interfaces/source'; +import { CreateSource, QuerySources, UpdateSource } from '../interfaces/source'; import { ResponseHelper } from '../utils/helpers/response-helper'; export class Source { @@ -9,7 +9,7 @@ export class Source { this.client = client; } - async all(query?: any) { + async all(query?: QuerySources) { try { const { data } = await this.client.httpGet(`/sources`, query); return data; @@ -18,36 +18,36 @@ export class Source { } } - async create(attributes: CreateSource, query?: any) { + async create(attributes: CreateSource) { try { - const { data } = await this.client.httpPost(`/sources`, attributes, query); + const { data } = await this.client.httpPost(`/sources`, attributes); return data; } catch (error) { ResponseHelper.handleErrors(error); } } - async find(id: string, query?: any) { + async find(id: string) { try { - const { data } = await this.client.httpGet(`/sources/${id}`, query); + const { data } = await this.client.httpGet(`/sources/${id}`); return data; } catch (error) { ResponseHelper.handleErrors(error); } } - async update(id: string, attributes: UpdateSource, query?: any) { + async update(id: string, attributes: UpdateSource) { try { - const { data } = await this.client.httpPut(`/sources/${id}`, attributes, query); + const { data } = await this.client.httpPut(`/sources/${id}`, attributes); return data; } catch (error) { ResponseHelper.handleErrors(error); } } - async delete(id: string, attributes?: any, query?: any) { + async delete(id: string) { try { - const { data } = await this.client.httpDelete(`/sources/${id}`, attributes, query); + const { data } = await this.client.httpDelete(`/sources/${id}`); return data; } catch (error) { ResponseHelper.handleErrors(error); diff --git a/src/Api/subscription.ts b/src/Api/subscription.ts index 521707d..b0154d9 100644 --- a/src/Api/subscription.ts +++ b/src/Api/subscription.ts @@ -1,5 +1,5 @@ import { Client } from '../client'; -import { CreateSubscription, UpdateSubscription } from '../interfaces/subscription'; +import { CreateSubscription, QuerySubscriptions, UpdateSubscription } from '../interfaces/subscription'; import { ResponseHelper } from '../utils/helpers/response-helper'; export class Subscription { @@ -9,7 +9,7 @@ export class Subscription { this.client = client; } - async all(query?: any) { + async all(query?: QuerySubscriptions) { try { const { data } = await this.client.httpGet(`/subscriptions`, query); return data; @@ -18,9 +18,9 @@ export class Subscription { } } - async create(attributes: CreateSubscription, query?: any) { + async create(attributes: CreateSubscription) { try { - const { data } = await this.client.httpPost(`/subscriptions`, attributes, query); + const { data } = await this.client.httpPost(`/subscriptions`, attributes); return data; } catch (error) { ResponseHelper.handleErrors(error); @@ -36,18 +36,18 @@ export class Subscription { } } - async update(id: string, attributes: UpdateSubscription, query?: any) { + async update(id: string, attributes: UpdateSubscription) { try { - const { data } = await this.client.httpPut(`/subscriptions/${id}`, attributes, query); + const { data } = await this.client.httpPut(`/subscriptions/${id}`, attributes); return data; } catch (error) { ResponseHelper.handleErrors(error); } } - async delete(id: string, attributes?: any, query?: any) { + async delete(id: string) { try { - const { data } = await this.client.httpDelete(`/subscriptions/${id}`, attributes, query); + const { data } = await this.client.httpDelete(`/subscriptions/${id}`); return data; } catch (error) { ResponseHelper.handleErrors(error); diff --git a/src/client.ts b/src/client.ts index 90e9e96..04d2bdf 100644 --- a/src/client.ts +++ b/src/client.ts @@ -65,7 +65,18 @@ export class Client { } private buildQueryParams(query: any) { - return new URLSearchParams(query).toString(); + const params = new URLSearchParams(); + for (const k of Object.keys(query)) { + if (Array.isArray(query[k])) { + for (let i = 0; i < query[k].length; i++) { + params.append(k, query[k][i]); + } + } else { + params.append(k, query[k]); + } + } + + return params.toString(); } private hasQueryParameters(query: any): boolean { diff --git a/src/interfaces/portal-links.ts b/src/interfaces/portal-links.ts index dd9cd3e..f35fc8a 100644 --- a/src/interfaces/portal-links.ts +++ b/src/interfaces/portal-links.ts @@ -1,3 +1,5 @@ +import { Pageable } from '../Api/common'; + export interface CreatePortalLink { name: string; endpoints: string[]; @@ -6,3 +8,7 @@ export interface CreatePortalLink { } export interface UpdatePortalLink extends CreatePortalLink {} + +export interface QueryPortalLinks extends Pageable { + endpointId?: string[]; +} diff --git a/src/interfaces/source.ts b/src/interfaces/source.ts index de0eb9b..de24ded 100644 --- a/src/interfaces/source.ts +++ b/src/interfaces/source.ts @@ -1,3 +1,5 @@ +import { Pageable } from '../Api/common'; + export interface CreateSource { created_at: Date; deleted_at: number; @@ -31,3 +33,8 @@ export interface CreateSource { } export interface UpdateSource extends CreateSource {} + +export interface QuerySources extends Pageable { + type?: string; + provider?: string; +} diff --git a/src/interfaces/subscription.ts b/src/interfaces/subscription.ts index f1f54a5..169c6e8 100644 --- a/src/interfaces/subscription.ts +++ b/src/interfaces/subscription.ts @@ -1,3 +1,5 @@ +import { Pageable } from '../Api/common'; + export interface CreateSubscription { name: string; type: 'cli' | 'api'; @@ -20,3 +22,7 @@ export interface CreateSubscription { } export interface UpdateSubscription extends CreateSubscription {} + +export interface QuerySubscriptions extends Pageable { + endpointId?: string[]; +}