Skip to content

Commit

Permalink
feat(type): chat
Browse files Browse the repository at this point in the history
  • Loading branch information
njfamirm committed Dec 30, 2022
1 parent 93a7867 commit 4b7ac95
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 38 deletions.
7 changes: 6 additions & 1 deletion core/type/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@alwatr/type",
"version": "0.27.0",
"description": "Alwatr Types",
"description": "Alwatr Global Types",
"private": true,
"keywords": [
"type",
Expand All @@ -10,6 +10,8 @@
"alwatr"
],
"type": "module",
"main": "type.js",
"types": "type.d.ts",
"author": "S. Ali Mihandoost <ali.mihandoost@gmail.com> (https://ali.mihandoost.com)",
"license": "MIT",
"files": [
Expand All @@ -26,5 +28,8 @@
"homepage": "https://github.com/AliMD/alwatr/tree/main/core/type#readme",
"bugs": {
"url": "https://github.com/AliMD/alwatr/issues"
},
"devDependencies": {
"@alwatr/fetch": "~0.27.0"
}
}
20 changes: 20 additions & 0 deletions core/type/src/chat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type {AlwatrDocumentStorage, AlwatrDocumentObject} from '@alwatr/fetch/type.js';

type CommonMessage = AlwatrDocumentObject & {
from: string;
replyId?: string;
};

export type ChatTextMessage = CommonMessage & {
type: 'text';
text: string;
};

export type ChatPhotoMessage = CommonMessage & {
type: 'photo';
photo: unknown;
};

export type ChatMessage = ChatTextMessage | ChatPhotoMessage;

export type ChatStorage = AlwatrDocumentStorage<ChatTextMessage>;
1 change: 1 addition & 0 deletions core/type/src/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './chat.js';
4 changes: 3 additions & 1 deletion core/type/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
// files, include and exclude from the inheriting config are always overwritten.
"include": ["src/**/*.ts"],
"exclude": [],
"references": []
"references": [
{"path": "../fetch"}
]
}
1 change: 1 addition & 0 deletions services/comment/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"devDependencies": {
"@alwatr/logger": "^0.27.0",
"@alwatr/nano-server": "^0.27.0",
"@alwatr/type": "^0.27.0",
"@alwatr/storage-client": "^0.27.0",
"@types/node": "~18.11.18",
"esbuild": "~0.16.11",
Expand Down
6 changes: 4 additions & 2 deletions services/comment/src/lib/storage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {AlwatrStorageClient} from '@alwatr/storage-client';

import {config} from '../config.js';
import {Message} from './type.js';

export const storageClient = new AlwatrStorageClient<Message>(config.storage);
import type {ChatMessage} from '@alwatr/type';


export const storageClient = new AlwatrStorageClient<ChatMessage>(config.storage);
18 changes: 0 additions & 18 deletions services/comment/src/lib/type.ts

This file was deleted.

4 changes: 2 additions & 2 deletions services/comment/src/route/patch.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {config, logger} from '../config.js';
import {nanoServer} from '../lib/nano-server.js';
import {storageClient} from '../lib/storage.js';
import {Message} from '../lib/type.js';

import type {AlwatrConnection, AlwatrServiceResponse} from '@alwatr/nano-server';
import type {ChatMessage} from '@alwatr/type';

nanoServer.route('PATCH', '/', setComment);

Expand All @@ -14,7 +14,7 @@ async function setComment(connection: AlwatrConnection): Promise<AlwatrServiceRe

const params = connection.requireQueryParams<{storage: string}>({storage: 'string'});

const bodyJson = await connection.requireJsonBody<Message>();
const bodyJson = await connection.requireJsonBody<ChatMessage>();

bodyJson.id ??= 'auto_increment';

Expand Down
1 change: 1 addition & 0 deletions services/comment/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
{"path": "../../core/nano-server"},
{"path": "../../core/logger"},
{"path": "../../core/storage-client"},
{"path": "../../core/type"},
]
}
3 changes: 3 additions & 0 deletions ui/ui-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,8 @@
"@alwatr/element": "^0.27.0",
"@alwatr/fetch": "^0.27.0",
"tslib": "~2.4.1"
},
"devDependencies": {
"@alwatr/type": "^0.27.0"
}
}
6 changes: 1 addition & 5 deletions ui/ui-kit/src/chat/chat-list.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import {AlwatrDummyElement, css, customElement, html, nothing, property} from '@alwatr/element';
import './chat-message.js';

import type {ChatMessage as _ChatMessage} from './chat-message.js';
import type {AlwatrDocumentObject, AlwatrDocumentStorage} from '@alwatr/fetch/type.js';
import type {ChatStorage} from '@alwatr/type';

declare global {
interface HTMLElementTagNameMap {
'alwatr-chat-list': AlwatrChatList;
}
}

export type ChatMessage = AlwatrDocumentObject & _ChatMessage;
export type ChatStorage = AlwatrDocumentStorage<ChatMessage>;

export function* map<T>(
items: Record<string, T> | undefined,
func: (value: T, index: string) => unknown,
Expand Down
11 changes: 3 additions & 8 deletions ui/ui-kit/src/chat/chat-message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import {AlwatrDummyElement, css, customElement, html, nothing, property, DirectionMixin} from '@alwatr/element';

import type {ChatTextMessage} from '@alwatr/type';

import './chat-avatar.js';
import './chat-bubble.js';

Expand All @@ -8,14 +11,6 @@ declare global {
}
}

export type ChatTextMessage = {
from: string;
type: 'text';
text: string;
};

export type ChatMessage = ChatTextMessage; // TODO: ChatPhotoMessage

/**
* Alwatr chat message box element.
*
Expand Down
2 changes: 1 addition & 1 deletion ui/ui-kit/src/chat/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {customElement, AlwatrSmartElement, css, html} from '@alwatr/element';
import './chat-footer.js';
import './chat-list.js';

import type {ChatStorage} from './chat-list.js';
import type {ChatStorage} from '@alwatr/type';

declare global {
interface HTMLElementTagNameMap {
Expand Down
1 change: 1 addition & 0 deletions ui/ui-kit/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
{"path": "../element"},
{"path": "../icon"},
{"path": "../../core/fetch"},
{"path": "../../core/type"},
]
}

0 comments on commit 4b7ac95

Please sign in to comment.