Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
3 changes: 2 additions & 1 deletion app/definitions/rest/v1/directory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { PaginatedResult } from '@rocket.chat/rest-typings';

import { type IServerRoom } from '../../IRoom';
import { type PaginatedResult } from '../helpers/PaginatedResult';

export type DirectoryEndpoint = {
directory: {
Expand Down
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
3 changes: 2 additions & 1 deletion app/definitions/rest/v1/settings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { PaginatedResult } from '@rocket.chat/rest-typings';

import { type ISetting, type ISettingColor } from '../../ISetting';
import { type PaginatedResult } from '../helpers/PaginatedResult';

type SettingsUpdateProps = SettingsUpdatePropDefault | SettingsUpdatePropsActions | SettingsUpdatePropsColor;

Expand Down
3 changes: 2 additions & 1 deletion app/definitions/rest/v1/teams.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 IServerRoom } from '../../IRoom';
import { type IServerTeamUpdateRoom, type ITeam, type TEAM_TYPE } from '../../ITeam';
import { type PaginatedResult } from '../helpers/PaginatedResult';

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

import {
type VideoConfCall,
type VideoConfCancelProps,
Expand All @@ -9,7 +11,6 @@ import {
type VideoConfListProps,
type VideoConfStartProps
} from '../../IVideoConference';
import { type PaginatedResult } from '../helpers/PaginatedResult';

export type VideoConferenceEndpoints = {
'video-conference.start': {
Expand Down
10 changes: 5 additions & 5 deletions app/lib/hooks/useEndpointData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ export const useEndpointData = <TPath extends PathFor<'GET'>>(
setLoading(true);
sdk
.get(endpoint, params as any)
.then(e => {
.then((e: any) => {
setLoading(false);
if (e.success) {
setResult(e);
} else {
if (e && typeof e === 'object' && 'success' in e && !e.success) {
setError(e as ErrorResult);
} else {
setResult(e as Serialized<ResultFor<'GET', MatchPathPattern<TPath>>>);
}
})
.catch((e: ErrorResult) => {
Expand All @@ -66,4 +66,4 @@ export const useEndpointData = <TPath extends PathFor<'GET'>>(
reload: fetchData,
error
};
};
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@
"@react-navigation/elements": "^2.6.1",
"@react-navigation/native": "^7.1.16",
"@react-navigation/native-stack": "^7.3.23",
"@rocket.chat/core-typings": "6.13.1",
"@rocket.chat/message-parser": "^0.31.31",
"@rocket.chat/mobile-crypto": "RocketChat/rocket.chat-mobile-crypto",
"@rocket.chat/rest-typings": "6.13.1",
"@rocket.chat/sdk": "RocketChat/Rocket.Chat.js.SDK#mobile",
"@rocket.chat/ui-kit": "0.31.19",
"bytebuffer": "5.0.1",
Expand Down
Loading