Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 3 additions & 17 deletions src/resources/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
ActionCreateData,
ActionUpdateData,
} from '../types/actions.js';
import { unwrapSingle, buildListParams as sharedBuildListParams } from './utils.js';

/**
* Actions resource operations
Expand Down Expand Up @@ -49,10 +50,7 @@ export class ActionsResource {
*/
async get(id: number): Promise<Action> {
const response = await this.httpClient.request<Action | { actions: Action[] }>(`/Actions/${id}`);
const action =
response && typeof response === 'object' && 'actions' in response && Array.isArray(response.actions)
? response.actions[0]
: (response as Action | undefined);
const action = unwrapSingle<Action>(response, 'actions');
if (!action) {
throw new Error(`Action ${id} not found`);
}
Expand Down Expand Up @@ -102,18 +100,6 @@ export class ActionsResource {
* Build query parameters from list params
*/
private buildListParams<T extends object>(params?: T): Record<string, string | number | boolean | undefined> {
if (!params) return {};

const result: Record<string, string | number | boolean | undefined> = {};
for (const [key, value] of Object.entries(params)) {
if (value !== undefined) {
const apiKey = key.replace(/([A-Z])/g, '_$1').toLowerCase();
result[apiKey] = value as string | number | boolean;
}
}
if (result.page_size !== undefined || result.page_no !== undefined) {
result.pageinate = true;
}
return result;
return sharedBuildListParams(params);
}
}
21 changes: 9 additions & 12 deletions src/resources/agents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
AgentCreateData,
AgentUpdateData,
} from '../types/agents.js';
import { unwrapSingle, buildListParams as sharedBuildListParams } from './utils.js';

/**
* Agents resource operations
Expand Down Expand Up @@ -48,11 +49,16 @@ export class AgentsResource {
* Get a single agent by ID
*/
async get(id: number): Promise<Agent> {
const response = await this.httpClient.request<{ agents: Agent[] }>(`/Agent/${id}`);
const agent = response.agents[0];
const response = await this.httpClient.request<Agent | { agents: Agent[] }>(`/Agent/${id}`);

const agent = unwrapSingle<Agent>(response, 'agents');

if (!agent) {

throw new Error(`Agent ${id} not found`);

}

return agent;
}

Expand Down Expand Up @@ -111,15 +117,6 @@ export class AgentsResource {
* Build query parameters from list params
*/
private buildListParams<T extends object>(params?: T): Record<string, string | number | boolean | undefined> {
if (!params) return {};

const result: Record<string, string | number | boolean | undefined> = {};
for (const [key, value] of Object.entries(params)) {
if (value !== undefined) {
const apiKey = key.replace(/([A-Z])/g, '_$1').toLowerCase();
result[apiKey] = value as string | number | boolean;
}
}
return result;
return sharedBuildListParams(params);
}
}
21 changes: 9 additions & 12 deletions src/resources/appointments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
AppointmentCreateData,
AppointmentUpdateData,
} from '../types/appointments.js';
import { unwrapSingle, buildListParams as sharedBuildListParams } from './utils.js';

/**
* Appointments resource operations
Expand Down Expand Up @@ -48,11 +49,16 @@ export class AppointmentsResource {
* Get a single appointment by ID
*/
async get(id: number): Promise<Appointment> {
const response = await this.httpClient.request<{ appointments: Appointment[] }>(`/Appointment/${id}`);
const appointment = response.appointments[0];
const response = await this.httpClient.request<Appointment | { appointments: Appointment[] }>(`/Appointment/${id}`);

const appointment = unwrapSingle<Appointment>(response, 'appointments');

if (!appointment) {

throw new Error(`Appointment ${id} not found`);

}

return appointment;
}

Expand Down Expand Up @@ -99,15 +105,6 @@ export class AppointmentsResource {
* Build query parameters from list params
*/
private buildListParams<T extends object>(params?: T): Record<string, string | number | boolean | undefined> {
if (!params) return {};

const result: Record<string, string | number | boolean | undefined> = {};
for (const [key, value] of Object.entries(params)) {
if (value !== undefined) {
const apiKey = key.replace(/([A-Z])/g, '_$1').toLowerCase();
result[apiKey] = value as string | number | boolean;
}
}
return result;
return sharedBuildListParams(params);
}
}
30 changes: 16 additions & 14 deletions src/resources/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
AssetUpdateData,
} from '../types/assets.js';
import type { BaseListParams } from '../types/common.js';
import { unwrapSingle, buildListParams as sharedBuildListParams } from './utils.js';

/**
* Assets resource operations
Expand Down Expand Up @@ -51,11 +52,16 @@ export class AssetsResource {
* Get a single asset by ID
*/
async get(id: number): Promise<Asset> {
const response = await this.httpClient.request<{ assets: Asset[] }>(`/Asset/${id}`);
const asset = response.assets[0];
const response = await this.httpClient.request<Asset | { assets: Asset[] }>(`/Asset/${id}`);

const asset = unwrapSingle<Asset>(response, 'assets');

if (!asset) {

throw new Error(`Asset ${id} not found`);

}

return asset;
}

Expand Down Expand Up @@ -102,16 +108,7 @@ export class AssetsResource {
* Build query parameters from list params
*/
private buildListParams<T extends object>(params?: T): Record<string, string | number | boolean | undefined> {
if (!params) return {};

const result: Record<string, string | number | boolean | undefined> = {};
for (const [key, value] of Object.entries(params)) {
if (value !== undefined) {
const apiKey = key.replace(/([A-Z])/g, '_$1').toLowerCase();
result[apiKey] = value as string | number | boolean;
}
}
return result;
return sharedBuildListParams(params);
}
}

Expand All @@ -138,11 +135,16 @@ export class AssetTypesResource {
* Get a single asset type by ID
*/
async get(id: number): Promise<AssetType> {
const response = await this.httpClient.request<{ asset_types: AssetType[] }>(`/AssetType/${id}`);
const assetType = response.asset_types[0];
const response = await this.httpClient.request<AssetType | { asset_types: AssetType[] }>(`/AssetType/${id}`);

const assetType = unwrapSingle<AssetType>(response, 'asset_types');

if (!assetType) {

throw new Error(`Asset type ${id} not found`);

}

return assetType;
}

Expand Down
21 changes: 9 additions & 12 deletions src/resources/clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
ClientCreateData,
ClientUpdateData,
} from '../types/clients.js';
import { unwrapSingle, buildListParams as sharedBuildListParams } from './utils.js';

/**
* Clients resource operations
Expand Down Expand Up @@ -48,11 +49,16 @@ export class ClientsResource {
* Get a single client by ID
*/
async get(id: number): Promise<Client> {
const response = await this.httpClient.request<{ clients: Client[] }>(`/Client/${id}`);
const client = response.clients[0];
const response = await this.httpClient.request<Client | { clients: Client[] }>(`/Client/${id}`);

const client = unwrapSingle<Client>(response, 'clients');

if (!client) {

throw new Error(`Client ${id} not found`);

}

return client;
}

Expand Down Expand Up @@ -99,15 +105,6 @@ export class ClientsResource {
* Build query parameters from list params
*/
private buildListParams<T extends object>(params?: T): Record<string, string | number | boolean | undefined> {
if (!params) return {};

const result: Record<string, string | number | boolean | undefined> = {};
for (const [key, value] of Object.entries(params)) {
if (value !== undefined) {
const apiKey = key.replace(/([A-Z])/g, '_$1').toLowerCase();
result[apiKey] = value as string | number | boolean;
}
}
return result;
return sharedBuildListParams(params);
}
}
21 changes: 9 additions & 12 deletions src/resources/contacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
ContactCreateData,
ContactUpdateData,
} from '../types/contacts.js';
import { unwrapSingle, buildListParams as sharedBuildListParams } from './utils.js';

/**
* Contacts resource operations
Expand Down Expand Up @@ -48,11 +49,16 @@ export class ContactsResource {
* Get a single contact by ID
*/
async get(id: number): Promise<Contact> {
const response = await this.httpClient.request<{ users: Contact[] }>(`/Users/${id}`);
const contact = response.users[0];
const response = await this.httpClient.request<Contact | { users: Contact[] }>(`/Users/${id}`);

const contact = unwrapSingle<Contact>(response, 'users');

if (!contact) {

throw new Error(`Contact ${id} not found`);

}

return contact;
}

Expand Down Expand Up @@ -99,15 +105,6 @@ export class ContactsResource {
* Build query parameters from list params
*/
private buildListParams<T extends object>(params?: T): Record<string, string | number | boolean | undefined> {
if (!params) return {};

const result: Record<string, string | number | boolean | undefined> = {};
for (const [key, value] of Object.entries(params)) {
if (value !== undefined) {
const apiKey = key.replace(/([A-Z])/g, '_$1').toLowerCase();
result[apiKey] = value as string | number | boolean;
}
}
return result;
return sharedBuildListParams(params);
}
}
21 changes: 9 additions & 12 deletions src/resources/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
ContractCreateData,
ContractUpdateData,
} from '../types/contracts.js';
import { unwrapSingle, buildListParams as sharedBuildListParams } from './utils.js';

/**
* Contracts resource operations
Expand Down Expand Up @@ -48,11 +49,16 @@ export class ContractsResource {
* Get a single contract by ID
*/
async get(id: number): Promise<Contract> {
const response = await this.httpClient.request<{ contracts: Contract[] }>(`/ClientContract/${id}`);
const contract = response.contracts[0];
const response = await this.httpClient.request<Contract | { contracts: Contract[] }>(`/ClientContract/${id}`);

const contract = unwrapSingle<Contract>(response, 'contracts');

if (!contract) {

throw new Error(`Contract ${id} not found`);

}

return contract;
}

Expand Down Expand Up @@ -99,15 +105,6 @@ export class ContractsResource {
* Build query parameters from list params
*/
private buildListParams<T extends object>(params?: T): Record<string, string | number | boolean | undefined> {
if (!params) return {};

const result: Record<string, string | number | boolean | undefined> = {};
for (const [key, value] of Object.entries(params)) {
if (value !== undefined) {
const apiKey = key.replace(/([A-Z])/g, '_$1').toLowerCase();
result[apiKey] = value as string | number | boolean;
}
}
return result;
return sharedBuildListParams(params);
}
}
21 changes: 9 additions & 12 deletions src/resources/invoices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
InvoiceCreateData,
InvoiceUpdateData,
} from '../types/invoices.js';
import { unwrapSingle, buildListParams as sharedBuildListParams } from './utils.js';

/**
* Invoices resource operations
Expand Down Expand Up @@ -48,11 +49,16 @@ export class InvoicesResource {
* Get a single invoice by ID
*/
async get(id: number): Promise<Invoice> {
const response = await this.httpClient.request<{ invoices: Invoice[] }>(`/Invoice/${id}`);
const invoice = response.invoices[0];
const response = await this.httpClient.request<Invoice | { invoices: Invoice[] }>(`/Invoice/${id}`);

const invoice = unwrapSingle<Invoice>(response, 'invoices');

if (!invoice) {

throw new Error(`Invoice ${id} not found`);

}

return invoice;
}

Expand Down Expand Up @@ -108,15 +114,6 @@ export class InvoicesResource {
* Build query parameters from list params
*/
private buildListParams<T extends object>(params?: T): Record<string, string | number | boolean | undefined> {
if (!params) return {};

const result: Record<string, string | number | boolean | undefined> = {};
for (const [key, value] of Object.entries(params)) {
if (value !== undefined) {
const apiKey = key.replace(/([A-Z])/g, '_$1').toLowerCase();
result[apiKey] = value as string | number | boolean;
}
}
return result;
return sharedBuildListParams(params);
}
}
Loading
Loading