From 87f20d553722d6d32c0e4dbc9cdd0b425d854309 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20=C5=A0vec?= <74354294+svecs132@users.noreply.github.com> Date: Mon, 16 Jun 2025 22:52:58 +0200 Subject: [PATCH] organization of imports and exports plus songs (#1) * imports and exports organization * songs --- README.md | 2 +- example.ts | 4 ++-- jsr.json | 2 +- mod.ts | 3 ++- src/index.ts | 2 +- src/services/arrangement.ts | 12 ++++++++++ src/services/folder.ts | 4 ++-- src/services/index.ts | 43 +++++++++++++++++++++++++++++++----- src/services/item.ts | 2 +- src/services/organization.ts | 2 +- src/services/person.ts | 2 +- src/services/plan.ts | 4 ++-- src/services/service_type.ts | 4 ++-- src/services/song.ts | 34 ++++++++++++++++++++++++++++ 14 files changed, 100 insertions(+), 20 deletions(-) create mode 100644 src/services/arrangement.ts create mode 100644 src/services/song.ts diff --git a/README.md b/README.md index df5d46d..be95593 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ client.getPeople().then((people) => { - [ ] recorded start time - [ ] get the current timer if you want - [ ] full item length - - [ ] end item on time + - [ ] to end item on time - [ ] live control - [ ] take/release control - [ ] restart / jump to end of plan diff --git a/example.ts b/example.ts index 15f592d..e1310e8 100644 --- a/example.ts +++ b/example.ts @@ -5,6 +5,6 @@ let services = new ServicesClient({ secret: process.env["PCO_SECRET"] || "", }); -const folders = await services.getFolders({ per_page: 100 }); +const songs = await services.getSongs({ per_page: 100 }); -console.log(folders.join("\n")); +console.log(songs.join("\n")); diff --git a/jsr.json b/jsr.json index 4209c49..1d09663 100644 --- a/jsr.json +++ b/jsr.json @@ -1,7 +1,7 @@ { "$schema": "https://jsr.io/schema/config-file.v1.json", "name": "@svecs132/pco-api", - "version": "0.0.2", + "version": "0.0.3", "license": "MIT", "exports": "./mod.ts", "publish": { diff --git a/mod.ts b/mod.ts index 02efda9..cc2c7a1 100644 --- a/mod.ts +++ b/mod.ts @@ -1 +1,2 @@ -export { ServicesClient } from "./src/services"; +export { default as ServicesClient } from "./src/services"; +export * as services from "./src/services"; diff --git a/src/index.ts b/src/index.ts index 3bdd67f..369d825 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,7 +7,7 @@ export type Options = { appId: string; secret: string; debug?: boolean }; export abstract class Client { private readonly appId: string; private readonly secret: string; - private debug: boolean = false; + private debug: boolean; abstract readonly baseUrl: string; constructor(options: Options) { diff --git a/src/services/arrangement.ts b/src/services/arrangement.ts new file mode 100644 index 0000000..d9b73bc --- /dev/null +++ b/src/services/arrangement.ts @@ -0,0 +1,12 @@ +import { Resource, Client } from "./.."; +import * as types from "../types"; + +export default class Arrangement extends Resource { + constructor(client: Client, data: types.Arrangement) { + super(client, data); + } + + toString(): string { + return `(\x1b[33mArrangement\x1b[0m \x1b[2m:id\x1b[0m ${this.id} \x1b[2m:name\x1b[0m ${this.attributes.name})`; + } +} diff --git a/src/services/folder.ts b/src/services/folder.ts index af1bd82..71b2020 100644 --- a/src/services/folder.ts +++ b/src/services/folder.ts @@ -1,9 +1,9 @@ import { Resource, Client, type RequestPagination, paginate } from "./.."; import * as types from "../types"; -import { ServiceType } from "./service_type"; +import ServiceType from "./service_type"; -export class Folder extends Resource { +export default class Folder extends Resource { constructor(client: Client, data: types.Folder) { super(client, data); } diff --git a/src/services/index.ts b/src/services/index.ts index 7facc1e..0df0c99 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -7,12 +7,16 @@ import { } from "./.."; import * as types from "../types"; -import { Folder } from "./folder"; -import { Organization } from "./organization"; -import { Person } from "./person"; -import { ServiceType } from "./service_type"; +import Arrangement from "./arrangement"; +import Folder from "./folder"; +import Item from "./item"; +import Organization from "./organization"; +import Person from "./person"; +import Plan from "./plan"; +import ServiceType from "./service_type"; +import Song from "./song"; -export class ServicesClient extends Client { +export default class ServicesClient extends Client { readonly baseUrl: string = `${BASE_URL}/services/v2`; constructor(options: Options) { @@ -78,4 +82,33 @@ export class ServicesClient extends Client { } return new ServiceType(this, res.data); } + + public async getSongs(pagination: RequestPagination = {}): Promise { + const path = `songs?${paginate(pagination)}`; + const res = await this.fetch(path); + if ("errors" in res) { + throw new Error(`Error fetching songs: ${res.errors}`); + } + return res.data.map((data) => new Song(this, data)); + } + + public async getSong(id: string): Promise { + const path = `songs/${id}`; + const res = await this.fetch(path); + if ("errors" in res) { + throw new Error(`Error fetching song: ${res.errors}`); + } + return new Song(this, res.data); + } } + +export { + Arrangement, + Folder, + Item, + Organization, + Person, + Plan, + ServiceType, + Song, +}; diff --git a/src/services/item.ts b/src/services/item.ts index 5824ac0..d180c52 100644 --- a/src/services/item.ts +++ b/src/services/item.ts @@ -1,7 +1,7 @@ import { Resource, Client } from "./.."; import * as types from "../types"; -export class Item extends Resource { +export default class Item extends Resource { constructor(client: Client, data: types.Item) { super(client, data); } diff --git a/src/services/organization.ts b/src/services/organization.ts index 9d8ce79..e2cf468 100644 --- a/src/services/organization.ts +++ b/src/services/organization.ts @@ -1,7 +1,7 @@ import { Resource, Client } from "./.."; import * as types from "../types"; -export class Organization extends Resource { +export default class Organization extends Resource { constructor(client: Client, data: types.Organization) { super(client, data); } diff --git a/src/services/person.ts b/src/services/person.ts index bb3ab64..6711edf 100644 --- a/src/services/person.ts +++ b/src/services/person.ts @@ -1,7 +1,7 @@ import { Resource, Client } from "./.."; import * as types from "../types"; -export class Person extends Resource { +export default class Person extends Resource { constructor(client: Client, data: types.Person) { super(client, data); } diff --git a/src/services/plan.ts b/src/services/plan.ts index 483efc0..ca29460 100644 --- a/src/services/plan.ts +++ b/src/services/plan.ts @@ -1,9 +1,9 @@ import { Resource, Client, type RequestPagination, paginate } from "./.."; import * as types from "../types"; -import { Item } from "./item"; +import Item from "./item"; -export class Plan extends Resource { +export default class Plan extends Resource { private serviceTypeId: string; constructor(client: Client, data: types.Plan) { diff --git a/src/services/service_type.ts b/src/services/service_type.ts index 3179d5b..ce4c7c7 100644 --- a/src/services/service_type.ts +++ b/src/services/service_type.ts @@ -1,9 +1,9 @@ import { Resource, Client, type RequestPagination, paginate } from "./.."; import * as types from "../types"; -import { Plan } from "./plan"; +import Plan from "./plan"; -export class ServiceType extends Resource { +export default class ServiceType extends Resource { constructor(client: Client, data: types.ServiceType) { super(client, data); } diff --git a/src/services/song.ts b/src/services/song.ts new file mode 100644 index 0000000..ef41277 --- /dev/null +++ b/src/services/song.ts @@ -0,0 +1,34 @@ +import { Resource, Client, type RequestPagination, paginate } from "./.."; +import * as types from "../types"; + +import Arrangement from "./arrangement"; + +export default class Song extends Resource { + constructor(client: Client, data: types.Song) { + super(client, data); + } + + toString(): string { + return `(\x1b[33mSong\x1b[0m \x1b[2m:id\x1b[0m ${this.id} \x1b[2m:title\x1b[0m ${this.attributes.title})`; + } + + public async getArrangements( + pagination: RequestPagination = {} + ): Promise { + const path = `songs/${this.id}/arrangements?${paginate(pagination)}`; + const res = await this.client.fetch(path); + if ("errors" in res) { + throw new Error(`Error fetching arrangements: ${res.errors}`); + } + return res.data.map((data) => new Arrangement(this.client, data)); + } + + public async getArrangement(id: string): Promise { + const path = `songs/${this.id}/arrangements/${id}`; + const res = await this.client.fetch(path); + if ("errors" in res) { + throw new Error(`Error fetching arrangement: ${res.errors}`); + } + return new Arrangement(this.client, res.data); + } +}