Compare commits
4 Commits
5dad7ab0ad
...
137976d2d7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
137976d2d7 | ||
|
|
3c553ceb71 | ||
|
|
7e0c3f3684 | ||
|
|
279f3a1c70 |
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "chatenium-sdk-typescript",
|
||||
"version": "1.0.0",
|
||||
"version": "2.0.0-beta.1",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "chatenium-sdk-typescript",
|
||||
"version": "1.0.0",
|
||||
"version": "2.0.0-beta.1",
|
||||
"license": "GPL-3.0-or-later",
|
||||
"dependencies": {
|
||||
"@openapitools/openapi-generator-cli": "^2.42.0"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "chatenium-sdk-typescript",
|
||||
"version": "1.0.0",
|
||||
"version": "2.0.0-beta.1",
|
||||
"description": "A TypeScript SDK for interacting with Chatenium API",
|
||||
"main": "index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
|
||||
72
src/index.ts
72
src/index.ts
@@ -1,16 +1,72 @@
|
||||
import {AuthApi, Configuration} from "./generated";
|
||||
import {DatabaseAPI} from "./storage/database";
|
||||
import {KeyringAPI} from "./storage/keyring";
|
||||
import {KeyValueAPI} from "./storage/keyvalue";
|
||||
import {
|
||||
AuthApi,
|
||||
AccountApi,
|
||||
Configuration,
|
||||
ContactsApi,
|
||||
DirectMessagingApi,
|
||||
FileTransferApi,
|
||||
CascadeApi, ThreadApi, ThreadMessage, ThreadMessagesApi, PicturesCreatorApi, PicturesApi, PictureCommentsApi,
|
||||
PictureAlbumApi, CascadeInviteApi, RanksApi
|
||||
} from "./generated";
|
||||
|
||||
export class ChateniumClient {
|
||||
public auth: AuthApi;
|
||||
public account: AccountApi;
|
||||
public contacts: ContactsApi;
|
||||
public directMessaging: DirectMessagingApi;
|
||||
public fileTransfers: FileTransferApi;
|
||||
public cascades: CascadeApi;
|
||||
public threads: ThreadApi;
|
||||
public threadMessages: ThreadMessagesApi;
|
||||
public pictureCreator: PicturesCreatorApi;
|
||||
public pictures: PicturesApi;
|
||||
public pictureComments: PictureCommentsApi;
|
||||
public pictureAlbums: PictureAlbumApi;
|
||||
public cascadeInvites: CascadeInviteApi;
|
||||
public cascadeRanks: RanksApi;
|
||||
|
||||
constructor(config: { baseUrl: string, storage: {database: DatabaseAPI, keyring: KeyringAPI, keyValue: KeyValueAPI} }) {
|
||||
const apiConfig = new Configuration({
|
||||
basePath: config.baseUrl+"/v3"
|
||||
private apiConfig: Configuration;
|
||||
|
||||
constructor(config: { baseUrl: string }) {
|
||||
this.apiConfig = new Configuration({
|
||||
basePath: config.baseUrl+"/v3",
|
||||
})
|
||||
|
||||
this.auth = new AuthApi(apiConfig);
|
||||
this.auth = new AuthApi(this.apiConfig);
|
||||
this.account = new AccountApi(this.apiConfig);
|
||||
this.contacts = new ContactsApi(this.apiConfig);
|
||||
this.directMessaging = new DirectMessagingApi(this.apiConfig);
|
||||
this.fileTransfers = new FileTransferApi(this.apiConfig);
|
||||
this.cascades = new CascadeApi(this.apiConfig);
|
||||
this.threads = new ThreadApi(this.apiConfig);
|
||||
this.pictureCreator = new PicturesCreatorApi(this.apiConfig);
|
||||
this.threadMessages = new ThreadMessagesApi(this.apiConfig);
|
||||
this.pictures = new PicturesApi(this.apiConfig);
|
||||
this.pictureComments = new PictureCommentsApi(this.apiConfig);
|
||||
this.pictureAlbums = new PictureAlbumApi(this.apiConfig);
|
||||
this.cascadeInvites = new CascadeInviteApi(this.apiConfig);
|
||||
this.cascadeRanks = new RanksApi(this.apiConfig);
|
||||
}
|
||||
|
||||
addHeader(name: string, value: string) {
|
||||
this.apiConfig.baseOptions.headers[name] = value;
|
||||
this.refreshHandlers();
|
||||
}
|
||||
|
||||
refreshHandlers() {
|
||||
this.auth = new AuthApi(this.apiConfig);
|
||||
this.account = new AccountApi(this.apiConfig);
|
||||
this.contacts = new ContactsApi(this.apiConfig);
|
||||
this.directMessaging = new DirectMessagingApi(this.apiConfig);
|
||||
this.fileTransfers = new FileTransferApi(this.apiConfig);
|
||||
this.cascades = new CascadeApi(this.apiConfig);
|
||||
this.threads = new ThreadApi(this.apiConfig);
|
||||
this.pictureCreator = new PicturesCreatorApi(this.apiConfig);
|
||||
this.threadMessages = new ThreadMessagesApi(this.apiConfig);
|
||||
this.pictures = new PicturesApi(this.apiConfig);
|
||||
this.pictureComments = new PictureCommentsApi(this.apiConfig);
|
||||
this.pictureAlbums = new PictureAlbumApi(this.apiConfig);
|
||||
this.cascadeInvites = new CascadeInviteApi(this.apiConfig);
|
||||
this.cascadeRanks = new RanksApi(this.apiConfig);
|
||||
}
|
||||
}
|
||||
32
src/services/contact.ts
Normal file
32
src/services/contact.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import {Contact, ContactsApi} from "../generated";
|
||||
|
||||
const EXCLUDED_METHODS = ['getContacts', 'newContact'] as const;
|
||||
type ExcludedMethods = typeof EXCLUDED_METHODS[number];
|
||||
const excludeSet = new Set<string>(EXCLUDED_METHODS);
|
||||
|
||||
type OmitContactIdArg<T> = T extends (first: any, ...args: infer Rest) => infer R
|
||||
? (...args: Rest) => R
|
||||
: T;
|
||||
|
||||
type WrappedDMHandler<T> = {
|
||||
[K in keyof T]: K extends ExcludedMethods
|
||||
? T[K]
|
||||
: T[K] extends (...args: any[]) => any
|
||||
? OmitContactIdArg<T[K]>
|
||||
: T[K];
|
||||
};
|
||||
|
||||
function createContactWrapper<TFirst>(
|
||||
instance: ContactsApi,
|
||||
boundArg: TFirst
|
||||
): WrappedDMHandler<ContactsApi> {
|
||||
return new Proxy(instance, {
|
||||
get(target, prop, receiver) {
|
||||
const value = Reflect.get(target, prop, receiver);
|
||||
if (typeof value === 'function' && !excludeSet.has(prop as string)) {
|
||||
return (...args: any[]) => value.call(target, boundArg, ...args);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}) as unknown as WrappedDMHandler<ContactsApi>;
|
||||
}
|
||||
21
src/services/dm.ts
Normal file
21
src/services/dm.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import {DirectMessagingApi} from "../generated";
|
||||
|
||||
type OmitContactIdArg<T> = T extends (first: any, ...args: infer Rest) => infer R
|
||||
? (...args: Rest) => R
|
||||
: T;
|
||||
|
||||
type WrappedDMHandler<T> = {
|
||||
[K in keyof T]: T[K] extends (...args: any[]) => any ? OmitContactIdArg<T[K]> : T[K];
|
||||
};
|
||||
|
||||
export function createDMHandler<TFirst>(instance: DirectMessagingApi, contactId: TFirst): WrappedDMHandler<DirectMessagingApi> {
|
||||
return new Proxy(instance, {
|
||||
get(target, prop, receiver) {
|
||||
const value = Reflect.get(target, prop, receiver);
|
||||
if (typeof value === 'function') {
|
||||
return (...args: any[]) => value.call(target, contactId, ...args);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}) as unknown as WrappedDMHandler<DirectMessagingApi>;
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
export interface DatabaseAPI {
|
||||
set(collection: string, key: string, value: any): void;
|
||||
get(collection: string, key: string): Promise<string>;
|
||||
delete(collection: string, key: string): void;
|
||||
flush(): void;
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
export interface KeyringAPI {
|
||||
set(key: string, value: any): void;
|
||||
get(key: string): Promise<string>;
|
||||
getAll(): Promise<string[]>;
|
||||
delete(key: string): void;
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
export interface KeyValueAPI {
|
||||
set(key: string, value: any): void;
|
||||
get(key: string): Promise<string>;
|
||||
delete(key: string): void;
|
||||
flush(): void;
|
||||
}
|
||||
Reference in New Issue
Block a user