Skip to content
Open
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
5 changes: 0 additions & 5 deletions app/definitions/rest/helpers/PaginatedRequest.ts

This file was deleted.

5 changes: 0 additions & 5 deletions app/definitions/rest/helpers/PaginatedResult.ts

This file was deleted.

127 changes: 107 additions & 20 deletions app/definitions/rest/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { type Endpoints } from '../v1';
// POC: This file implements rest-typings patterns adapted to work with our current Endpoints structure.
// Once all endpoint definitions are migrated to use @rocket.chat/rest-typings Endpoints
// (which uses /v1/ prefix paths), we can replace this with direct imports from the package.
import type { KeyOfEach } from '@rocket.chat/core-typings';
import type { ReplacePlaceholders } from '@rocket.chat/rest-typings';

type ReplacePlaceholders<TPath extends string> = string extends TPath
? TPath
: TPath extends `${infer Start}:${infer _Param}/${infer Rest}`
? `${Start}${string}/${ReplacePlaceholders<Rest>}`
: TPath extends `${infer Start}:${infer _Param}`
? `${Start}${string}`
: TPath;
import { type Endpoints } from '../v1';

type KeyOfEach<T> = T extends any ? keyof T : never;
// Re-export utility types from rest-typings that we can use directly
export type { ReplacePlaceholders } from '@rocket.chat/rest-typings';

// GetParams and GetResult - matching rest-typings implementation
type GetParams<TOperation> = TOperation extends (...args: any) => any
? Parameters<TOperation>[0] extends void
? void
Expand All @@ -26,7 +26,7 @@ type OperationsByPathPatternAndMethod<
? {
pathPattern: TPathPattern;
method: TMethod;
path: ReplacePlaceholders<TPathPattern>;
path: ReplacePlaceholders<TPathPattern & string>;
params: GetParams<Endpoints[TPathPattern][TMethod]>;
result: GetResult<Endpoints[TPathPattern][TMethod]>;
}
Expand All @@ -38,11 +38,10 @@ type OperationsByPathPattern<TPathPattern extends keyof Endpoints> = TPathPatter

type Operations = OperationsByPathPattern<keyof Endpoints>;

type Method = Operations['method'];

// Core types matching rest-typings structure
export type Method = Operations['method'];
export type PathPattern = Operations['pathPattern'];

type Path = Operations['path'];
export type Path = Operations['path'];

//

Expand All @@ -56,22 +55,108 @@ export type Serialized<T> = T extends Date
}
: null;

// MatchPathPattern - matching rest-typings implementation
export type MatchPathPattern<TPath extends Path> = TPath extends any
? Extract<Operations, { path: TPath }>['pathPattern']
: never;

export type OperationResult<
TMethod extends Method,
TPathPattern extends PathPattern
> = TMethod extends keyof Endpoints[TPathPattern] ? GetResult<Endpoints[TPathPattern][TMethod]> : never;
// JoinPathPattern - utility from rest-typings for combining path patterns
export type JoinPathPattern<
TBasePath extends string,
TSubPathPattern extends string
> = Extract<
PathPattern,
`${TBasePath}${TSubPathPattern extends '' ? TSubPathPattern : `/${TSubPathPattern}`}` | TSubPathPattern
>;

// UrlParams - extracts URL parameters from path patterns (from rest-typings)
export type UrlParams<T extends string> = string extends T
? Record<string, string>
: T extends `${string}:${infer Param}/${infer Rest}`
? {
[k in Param | keyof UrlParams<Rest>]: string;
}
: T extends `${string}:${infer Param}`
? {
[k in Param]: string;
}
: undefined | Record<string, never>;

// MethodOf - gets methods available for a path pattern (from rest-typings)
export type MethodOf<TPathPattern extends PathPattern> = TPathPattern extends any
? keyof Endpoints[TPathPattern]
: never;

// PathFor - matching rest-typings implementation pattern
type MethodToPathMap = {
[TOperation in Operations as TOperation['method']]: TOperation['path'];
};

export type PathFor<TMethod extends Method> = TMethod extends any ? Extract<Operations, { method: TMethod }>['path'] : never;
export type PathFor<TMethod extends Method> = MethodToPathMap[TMethod];

// PathWithParamsFor and PathWithoutParamsFor - additional utilities from rest-typings
// These check if the operation function has parameters (matching rest-typings pattern)
type MethodToPathWithParamsMap = {
[TOperation in Operations as TOperation['params'] extends void
? never
: TOperation['method']]: TOperation['path'];
};

type MethodToPathWithoutParamsMap = {
[TOperation in Operations as TOperation['params'] extends void
? TOperation['method']
: undefined extends TOperation['params']
? TOperation['method']
: never]: TOperation['path'];
};

export type PathWithParamsFor<TMethod extends Method> = MethodToPathWithParamsMap[TMethod extends keyof MethodToPathWithParamsMap
? TMethod
: never];

export type PathWithoutParamsFor<TMethod extends Method> = MethodToPathWithoutParamsMap[TMethod extends keyof MethodToPathWithoutParamsMap
? TMethod
: never];

// OperationParams and OperationResult - matching rest-typings implementation
export type OperationParams<
TMethod extends Method,
TPathPattern extends PathPattern
> = TMethod extends keyof Endpoints[TPathPattern] ? GetParams<Endpoints[TPathPattern][TMethod]> : never;

export type OperationResult<
TMethod extends Method,
TPathPattern extends PathPattern
> = TMethod extends keyof Endpoints[TPathPattern] ? GetResult<Endpoints[TPathPattern][TMethod]> : never;

// ParamsFor and ResultFor - using MethodToPathPatternToParamsMap pattern from rest-typings
type MethodToPathPatternToParamsMap = {
[TMethod in Method]: {
[TPathPattern in keyof Endpoints]: TMethod extends keyof Endpoints[TPathPattern]
? Endpoints[TPathPattern][TMethod] extends infer TOperation
? TOperation extends (...args: any) => any
? Parameters<TOperation>[0]
: never
: never
: never;
};
};

type MethodToPathPatternToResultMap = {
[TMethod in Method]: {
[TPathPattern in keyof Endpoints]: TMethod extends keyof Endpoints[TPathPattern]
? Endpoints[TPathPattern][TMethod] extends infer TOperation
? TOperation extends (...args: any) => any
? ReturnType<TOperation>
: never
: never
: never;
};
};

export type ParamsFor<TMethod extends Method, TPathPattern extends PathPattern> =
MethodToPathPatternToParamsMap[TMethod][TPathPattern];

type SuccessResult<T> = T & { success: true };

type FailureResult<T, TStack = undefined, TErrorType = undefined, TErrorDetails = undefined> = {
Expand All @@ -87,8 +172,10 @@ type UnauthorizedResult<T> = {
error: T | 'unauthorized';
};

// ResultFor wraps the base result with success/error handling (SDK-specific)
// Uses MethodToPathPatternToResultMap pattern from rest-typings
export type ResultFor<TMethod extends Method, TPathPattern extends PathPattern> =
| SuccessResult<OperationResult<TMethod, TPathPattern>>
| SuccessResult<MethodToPathPatternToResultMap[TMethod][TPathPattern]>
| FailureResult<unknown, unknown, unknown, unknown>
| UnauthorizedResult<unknown>;

Expand Down
3 changes: 2 additions & 1 deletion app/definitions/rest/v1/channels.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { PaginatedRequest } from '@rocket.chat/rest-typings';

import { type ITeam } from '../../ITeam';
import type { IMessageFromServer } from '../../IMessage';
import type { IServerRoom } from '../../IRoom';
import type { IUser } from '../../IUser';
import { type IGetRoomRoles } from '../../IRole';
import { type IServerAttachment } from '../../IAttachment';
import { type PaginatedRequest } from '../helpers/PaginatedRequest';

export type ChannelsEndpoints = {
'channels.files': {
Expand Down
3 changes: 2 additions & 1 deletion app/definitions/rest/v1/chat.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { PaginatedResult } from '@rocket.chat/rest-typings';

import type { EncryptedContent, IMessage, IMessageFromServer, IReadReceipts } from '../../IMessage';
import type { IServerRoom } from '../../IRoom';
import { type PaginatedResult } from '../helpers/PaginatedResult';

export type ChatEndpoints = {
'chat.getMessage': {
Expand Down
20 changes: 14 additions & 6 deletions app/definitions/rest/v1/customUserStatus.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
export type CustomUserStatusEndpoints = {
'custom-user-status.list': {
GET: (params: { query: string }) => {
statuses: unknown[];
};
};
import type { Endpoints } from '@rocket.chat/rest-typings';

type ExtractCustomUserStatusEndpoints<T> = {
[K in keyof T as K extends `/v1/custom-user-status.${string}` ? K : never]: T[K];
};

type RestTypingsCustomUserStatusEndpoints = ExtractCustomUserStatusEndpoints<Endpoints>;

type RemoveV1Prefix<T> = T extends `/v1/${infer Rest}` ? Rest : T;

type AdaptCustomUserStatusEndpoints<T> = {
[K in keyof T as RemoveV1Prefix<K & string>]: T[K];
};

export type CustomUserStatusEndpoints = AdaptCustomUserStatusEndpoints<RestTypingsCustomUserStatusEndpoints>;
26 changes: 14 additions & 12 deletions app/definitions/rest/v1/directory.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { type IServerRoom } from '../../IRoom';
import { type PaginatedResult } from '../helpers/PaginatedResult';

export type DirectoryEndpoint = {
directory: {
GET: (params: {
query: { [key: string]: string };
count: number;
offset: number;
sort: { [key: string]: number };
}) => PaginatedResult<{ result: IServerRoom[]; count: number }>;
};
import type { Endpoints } from '@rocket.chat/rest-typings';

type ExtractDirectoryEndpoint<T> = {
[K in keyof T as K extends `/v1/directory` ? K : never]: T[K];
};

type RestTypingsDirectoryEndpoint = ExtractDirectoryEndpoint<Endpoints>;

type RemoveV1Prefix<T> = T extends `/v1/${infer Rest}` ? Rest : T;

type AdaptDirectoryEndpoint<T> = {
[K in keyof T as RemoveV1Prefix<K & string>]: T[K];
};

export type DirectoryEndpoint = AdaptDirectoryEndpoint<RestTypingsDirectoryEndpoint>;
4 changes: 2 additions & 2 deletions app/definitions/rest/v1/emojiCustom.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { PaginatedRequest, PaginatedResult } from '@rocket.chat/rest-typings';

import type { ICustomEmojiDescriptor } from '../../ICustomEmojiDescriptor';
import { type PaginatedRequest } from '../helpers/PaginatedRequest';
import { type PaginatedResult } from '../helpers/PaginatedResult';

export type EmojiCustomEndpoints = {
'emoji-custom.all': {
Expand Down
3 changes: 2 additions & 1 deletion app/definitions/rest/v1/groups.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { PaginatedRequest } from '@rocket.chat/rest-typings';

import { type ITeam } from '../../ITeam';
import type { IMessageFromServer } from '../../IMessage';
import type { IServerRoom } from '../../IRoom';
import type { IUser } from '../../IUser';
import { type IGetRoomRoles } from '../../IRole';
import { type IServerAttachment } from '../../IAttachment';
import { type PaginatedRequest } from '../helpers/PaginatedRequest';

export type GroupsEndpoints = {
'groups.files': {
Expand Down
3 changes: 2 additions & 1 deletion app/definitions/rest/v1/im.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { PaginatedRequest } from '@rocket.chat/rest-typings';

import type { IMessageFromServer } from '../../IMessage';
import type { IServerRoom, RoomID, RoomType } from '../../IRoom';
import type { IUser } from '../../IUser';
import { type IServerAttachment } from '../../IAttachment';
import { type PaginatedRequest } from '../helpers/PaginatedRequest';

export type ImEndpoints = {
'im.create': {
Expand Down
4 changes: 2 additions & 2 deletions app/definitions/rest/v1/omnichannel.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { PaginatedRequest, PaginatedResult } from '@rocket.chat/rest-typings';

import { type ICannedResponse } from '../../ICannedResponse';
import { type ILivechatAgent } from '../../ILivechatAgent';
import { type ILivechatDepartment } from '../../ILivechatDepartment';
Expand All @@ -8,8 +10,6 @@ import { type ILivechatVisitor, type ILivechatVisitorDTO } from '../../ILivechat
import { type IMessage } from '../../IMessage';
import { type IOmnichannelRoom, type IServerRoom } from '../../IRoom';
import { type ISetting } from '../../ISetting';
import { type PaginatedRequest } from '../helpers/PaginatedRequest';
import { type PaginatedResult } from '../helpers/PaginatedResult';

type booleanString = 'true' | 'false';

Expand Down
20 changes: 6 additions & 14 deletions app/definitions/rest/v1/permissions.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import { type IPermission } from '../../IPermission';
import type { PermissionsEndpoints as RestTypingsPermissionsEndpoints } from '@rocket.chat/rest-typings';

type PermissionsUpdateProps = { permissions: { _id: string; roles: string[] }[] };
type RemoveV1Prefix<T> = T extends `/v1/${infer Rest}` ? Rest : T;

export type PermissionsEndpoints = {
'permissions.listAll': {
GET: (params: { updatedSince?: string }) => {
update: IPermission[];
remove: IPermission[];
};
};
'permissions.update': {
POST: (params: PermissionsUpdateProps) => {
permissions: IPermission[];
};
};
type AdaptPermissionsEndpoints<T> = {
[K in keyof T as RemoveV1Prefix<K & string>]: T[K];
};

export type PermissionsEndpoints = AdaptPermissionsEndpoints<RestTypingsPermissionsEndpoints>;
33 changes: 12 additions & 21 deletions app/definitions/rest/v1/push.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
type TPushInfo = {
pushGatewayEnabled: boolean;
defaultPushGateway: boolean;
success: boolean;
import type { Endpoints } from '@rocket.chat/rest-typings';

type ExtractPushEndpoints<T> = {
[K in keyof T as K extends `/v1/push.${string}` ? K : never]: T[K];
};

export type PushEndpoints = {
'push.token': {
POST: (params: { value: string; type: string; appName: string }) => {
result: {
id: string;
token: string;
appName: string;
userId: string;
};
};
};
'push.info': {
GET: () => TPushInfo;
};
'push.test': {
POST: () => { tokensCount: number };
};
type RestTypingsPushEndpoints = ExtractPushEndpoints<Endpoints>;

type RemoveV1Prefix<T> = T extends `/v1/${infer Rest}` ? Rest : T;

type AdaptPushEndpoints<T> = {
[K in keyof T as RemoveV1Prefix<K & string>]: T[K];
};

export type PushEndpoints = AdaptPushEndpoints<RestTypingsPushEndpoints>;
Loading