diff --git a/README.md b/README.md index 4dd503dc..cb0f0def 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ async function getMe() { 2. Receive Mixin Messenger messages ```js -const { MixinApi } = require('@mixin.dev/mixin-node-sdk'); +const { BlazeKeystoreClient } = require('@mixin.dev/mixin-node-sdk/blaze'); const keystore = { app_id: '', @@ -57,16 +57,13 @@ const keystore = { server_public_key: '', session_private_key: '', }; -const config = { - keystore, - blazeOptions: { - parse: true, - syncAck: true, - }, +const config = blazeOptions: { + parse: true, + syncAck: true, }; -const client = MixinApi(config); -client.blaze.loop({ +const client = BlazeKeystoreClient(keystore, config); +client.loop({ onMessage(msg) { console.log(msg); }, diff --git a/README.zh-CN.md b/README.zh-CN.md index 51d6a886..f12c3a79 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -47,7 +47,7 @@ async function getMe() { 2. 使用 Mixin 的消息功能 ```js -const { MixinApi } = require('@mixin.dev/mixin-node-sdk'); +const { BlazeKeystoreClient } = require('@mixin.dev/mixin-node-sdk/blaze'); const keystore = { app_id: '', @@ -55,16 +55,13 @@ const keystore = { server_public_key: '', session_private_key: '', }; -const config = { - keystore, - blazeOptions: { - parse: true, - syncAck: true, - }, +const config = blazeOptions: { + parse: true, + syncAck: true, }; -const client = MixinApi(config); -client.blaze.loop({ +const client = BlazeKeystoreClient(keystore, config); +client.loop({ onMessage(msg) { console.log(msg); }, diff --git a/example/blaze.ts b/example/blaze.ts index 6ff95d92..bafa86c0 100644 --- a/example/blaze.ts +++ b/example/blaze.ts @@ -1,12 +1,11 @@ const { MixinApi } = require('..'); +const { BlazeKeystoreClient } = require('../dist/blaze'); const keystore = require('../keystore.json'); // keystore from your bot -const client = MixinApi({ - keystore, - blazeOptions: { - parse: true, // parse the message content by sdk - syncAck: true, // automatically send read status after receiving message - }, +const client = MixinApi({ keystore }); +const blaze = BlazeKeystoreClient(keystore, { + parse: true, // parse the message content by sdk + syncAck: true, // automatically send read status after receiving message }); const handler = { @@ -41,4 +40,4 @@ const handler = { }, }; // ws will auto reconnect after connect closing -client.blaze.loop(handler); +blaze.loop(handler); diff --git a/example/message.ts b/example/message.ts index 47df8c21..b9b3b45f 100644 --- a/example/message.ts +++ b/example/message.ts @@ -7,8 +7,10 @@ const main = async () => { const client = MixinApi({ keystore }); + const conv = await client.conversation.createContact('opponent-id'); + const resp = await client.message.sendLegacy({ - conversation_id: '9451292c-c81c-4574-961a-ce9075e32400', + conversation_id: conv.conversation_id, message_id: v4(), category: 'PLAIN_TEXT', data_base64: base64RawURLEncode('hi'), diff --git a/package-lock.json b/package-lock.json index f124f938..71ad4eca 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,7 +18,6 @@ "bs58": "^6.0.0", "int64-buffer": "^1.0.1", "is-retry-allowed": "^3.0.0", - "lodash.merge": "^4.6.2", "md5": "^2.3.0", "nano-seconds": "^1.2.2", "pako": "^2.0.4", @@ -31,7 +30,6 @@ "@size-limit/esbuild": "^12.0.1", "@size-limit/esbuild-why": "^12.0.1", "@size-limit/preset-small-lib": "^12.0.1", - "@types/lodash.merge": "^4.6.7", "@types/md5": "^2.3.5", "@types/node": "^25.5.0", "@types/node-forge": "^1.3.8", @@ -2195,23 +2193,6 @@ "dev": true, "license": "MIT" }, - "node_modules/@types/lodash": { - "version": "4.17.24", - "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.17.24.tgz", - "integrity": "sha512-gIW7lQLZbue7lRSWEFql49QJJWThrTFFeIMJdp3eH4tKoxm1OvEPg02rm4wCCSHS0cL3/Fizimb35b7k8atwsQ==", - "dev": true, - "license": "MIT" - }, - "node_modules/@types/lodash.merge": { - "version": "4.6.9", - "resolved": "https://registry.npmjs.org/@types/lodash.merge/-/lodash.merge-4.6.9.tgz", - "integrity": "sha512-23sHDPmzd59kUgWyKGiOMO2Qb9YtqRO/x4IhkgNUiPQ1+5MUVqi6bCZeq9nBJ17msjIMbEIO5u+XW4Kz6aGUhQ==", - "dev": true, - "license": "MIT", - "dependencies": { - "@types/lodash": "*" - } - }, "node_modules/@types/md5": { "version": "2.3.6", "resolved": "https://registry.npmjs.org/@types/md5/-/md5-2.3.6.tgz", @@ -4637,12 +4618,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/lodash.merge": { - "version": "4.6.2", - "resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz", - "integrity": "sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==", - "license": "MIT" - }, "node_modules/magic-string": { "version": "0.30.21", "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.21.tgz", diff --git a/package.json b/package.json index 7911426c..87be1fe4 100644 --- a/package.json +++ b/package.json @@ -5,10 +5,28 @@ "description": "Mixin SDK for Node.js and Javascript", "main": "dist/index.js", "typings": "dist/index.d.ts", + "exports": { + ".": { + "types": "./dist/index.d.ts", + "import": "./dist/mixin-node-sdk.esm.mjs", + "require": "./dist/index.js", + "default": "./dist/index.js" + }, + "./blaze": { + "types": "./dist/blaze/index.d.ts", + "import": "./dist/blaze/index.mjs", + "require": "./dist/blaze/index.js", + "default": "./dist/blaze/index.js" + }, + "./package.json": "./package.json" + }, "files": [ "dist", - "src" + "README.md", + "README.zh-CN.md", + "LICENSE" ], + "sideEffects": false, "engines": { "node": ">=20" }, @@ -28,8 +46,8 @@ "js" ], "scripts": { - "start": "bunchee --watch --runtime node --format cjs --output dist/index.js", - "build": "rm -rf dist && bunchee --runtime node --format cjs --output dist/index.js && bunchee --runtime browser --format esm --output dist/mixin-node-sdk.esm.js --no-clean && bunchee --runtime browser --format cjs --output dist/mixin-node-sdk.cjs.production.min.js --minify --no-clean --no-dts", + "start": "bunchee --watch", + "build": "rm -rf dist && bunchee", "build:umd": "bunchee --runtime browser --format umd --output dist/mixin-node-sdk.umd.js", "test": "vitest run", "test:watch": "vitest", @@ -40,15 +58,15 @@ "analyze": "size-limit --why" }, "author": "contact@mixin.one", - "module": "dist/mixin-node-sdk.esm.js", + "module": "dist/mixin-node-sdk.esm.mjs", "size-limit": [ { - "path": "dist/mixin-node-sdk.cjs.production.min.js", - "limit": "90 KB" + "path": "dist/index.js", + "limit": "80 KB" }, { - "path": "dist/mixin-node-sdk.esm.js", - "limit": "75 KB" + "path": "dist/mixin-node-sdk.esm.mjs", + "limit": "65 KB" } ], "devDependencies": { @@ -56,7 +74,6 @@ "@size-limit/esbuild": "^12.0.1", "@size-limit/esbuild-why": "^12.0.1", "@size-limit/preset-small-lib": "^12.0.1", - "@types/lodash.merge": "^4.6.7", "@types/md5": "^2.3.5", "@types/node": "^25.5.0", "@types/node-forge": "^1.3.8", @@ -73,7 +90,7 @@ "jiti": "^2.6.1", "jsdom": "^28.1.0", "node-forge": "^1.3.1", - "prettier": "^3.3.3", + "prettier": "^3.8.1", "size-limit": "^12.0.1", "tslib": "^2.4.0", "typescript": "^5.9.3", @@ -89,7 +106,6 @@ "bs58": "^6.0.0", "int64-buffer": "^1.0.1", "is-retry-allowed": "^3.0.0", - "lodash.merge": "^4.6.2", "md5": "^2.3.0", "nano-seconds": "^1.2.2", "pako": "^2.0.4", diff --git a/src/blaze/client.ts b/src/blaze/client.ts index 4b9663a4..6a1535cc 100644 --- a/src/blaze/client.ts +++ b/src/blaze/client.ts @@ -57,7 +57,3 @@ export const BlazeKeystoreClient = (keystore: Keystore | undefined, wsOptions: B getWebSocket: () => ws, }; }; - -export const BlazeClient = (keystore: Keystore, wsOptions?: BlazeOptions) => ({ blaze: BlazeKeystoreClient(keystore, wsOptions) }); - -export default BlazeClient; diff --git a/src/blaze/type.ts b/src/blaze/type.ts index 76697803..cd702edf 100644 --- a/src/blaze/type.ts +++ b/src/blaze/type.ts @@ -1,8 +1,37 @@ -import { MessageView } from '../client/types/message'; +import { MessageCategory, MessageStatus } from '../client/types/message'; export type MessageType = MessageView | TransferView | SystemConversationPayload; -interface TransferView { +export interface TransferData { + type: 'transaction'; + snapshot_id: string; + transaction_hash: string; + user_id: string; + opponent_id: string; + asset_id: string; + amount: string; + memo: string; + created_at: string; +} + +export interface MessageView { + type: 'message'; + representative_id: string; + quote_message_id: string; + conversation_id: string; + user_id: string; + session_id: string; + message_id: string; + category: MessageCategory; + data: string | TransferData; + data_base64: string; + status: MessageStatus; + source: string; + created_at: string; + updated_at: string; +} + +export interface TransferView { type: 'transfer'; snapshot_id: string; counter_user_id: string; @@ -13,7 +42,7 @@ interface TransferView { created_at: string; } -interface SystemConversationPayload { +export interface SystemConversationPayload { action: string; participant_id: string; user_id?: string; diff --git a/src/blaze/utils.ts b/src/blaze/utils.ts index d5f24726..bfa0d3b7 100644 --- a/src/blaze/utils.ts +++ b/src/blaze/utils.ts @@ -2,8 +2,8 @@ import WebSocket from 'ws'; import { v4 as uuid } from 'uuid'; import { gzip, ungzip } from 'pako'; import type Keystore from '../client/types/keystore'; -import type { BlazeHandler, BlazeOptions, MessageView, BlazeMessage } from '../client/types'; -import { signAccessToken } from '../client/utils'; +import type { MessageView, BlazeHandler, BlazeOptions, BlazeMessage } from './type'; +import { signAccessToken } from '../client/utils/auth'; export function websocket( keystore: Keystore | undefined, diff --git a/src/client/http.ts b/src/client/http.ts index 96a54007..78429237 100644 --- a/src/client/http.ts +++ b/src/client/http.ts @@ -8,9 +8,6 @@ import type { RequestConfig } from './types/client'; import { ResponseError } from './error'; import { signAccessToken } from './utils/auth'; -axios.defaults.headers.post['Content-Type'] = 'application/json'; -axios.defaults.headers.put['Content-Type'] = 'application/json'; -axios.defaults.headers.patch['Content-Type'] = 'application/json'; export function http(keystore?: Keystore, config?: RequestConfig): AxiosInstance { const timeout = config?.timeout || 3000; const retries = config?.retry || 5; @@ -19,6 +16,12 @@ export function http(keystore?: Keystore, config?: RequestConfig): AxiosInstance baseURL: 'https://api.mixin.one', timeout, ...config, + headers: { + post: { 'Content-Type': 'application/json' }, + put: { 'Content-Type': 'application/json' }, + patch: { 'Content-Type': 'application/json' }, + ...(config?.headers ?? {}), + }, }); ins.interceptors.request.use((config: InternalAxiosRequestConfig) => { @@ -60,5 +63,3 @@ export function http(keystore?: Keystore, config?: RequestConfig): AxiosInstance return ins; } - -export const mixinRequest = http(); diff --git a/src/client/index.ts b/src/client/index.ts index d8996e3b..530d56dd 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -7,7 +7,6 @@ export { AddressClient } from './address'; export { AppClient } from './app'; export { AssetClient } from './asset'; export { AttachmentClient } from './attachment'; -export { BlazeClient } from '../blaze/client'; export { CircleClient } from './circle'; export { CodeClient } from './code'; export { CollectibleClient, MintMinimumCost, GroupMembers, GroupThreshold } from './collectible'; @@ -25,6 +24,5 @@ export { UserClient } from './user'; export { UtxoClient } from './utxo'; export * from './mixin-client'; -export { mixinRequest } from './http'; export { ResponseError } from './error'; export * from './utils'; diff --git a/src/client/mixin-client.ts b/src/client/mixin-client.ts index 8ad5fe24..8268dc1c 100644 --- a/src/client/mixin-client.ts +++ b/src/client/mixin-client.ts @@ -1,4 +1,3 @@ -import merge from 'lodash.merge'; import type { AxiosInstance } from 'axios'; import type Keystore from './types/keystore'; import type { HTTPConfig, RequestClient } from './types'; @@ -20,15 +19,13 @@ import { PaymentBaseClient } from './payment'; import { PinKeystoreClient } from './pin'; import { TransferKeystoreClient } from './transfer'; import { UserKeystoreClient } from './user'; -import { BlazeKeystoreClient } from '../blaze/client'; import { UtxoKeystoreClient } from './utxo'; import { SafeKeystoreClient } from './safe'; -const KeystoreClient = (axiosInstance: AxiosInstance, keystore: Keystore | undefined, config: HTTPConfig) => ({ +const KeystoreClient = (axiosInstance: AxiosInstance, keystore: Keystore | undefined) => ({ address: AddressKeystoreClient(axiosInstance, keystore), app: AppKeystoreClient(axiosInstance, keystore), asset: AssetKeystoreClient(axiosInstance), - blaze: BlazeKeystoreClient(keystore, config.blazeOptions), attachment: AttachmentKeystoreClient(axiosInstance), circle: CircleKeystoreClient(axiosInstance), code: CodeKeystoreClient(axiosInstance), @@ -55,7 +52,7 @@ export function MixinApi(config: HTTPConfig = {}): KeystoreClientReturnType & Re const axiosInstance = createAxiosClient(keystore, requestConfig); const requestClient = createRequestClient(axiosInstance); - const keystoreClient = KeystoreClient(axiosInstance, keystore, config); + const keystoreClient = KeystoreClient(axiosInstance, keystore); - return merge(keystoreClient, requestClient); + return { ...keystoreClient, ...requestClient }; } diff --git a/src/client/types/client.ts b/src/client/types/client.ts index e1d7a966..d7f0f39a 100644 --- a/src/client/types/client.ts +++ b/src/client/types/client.ts @@ -1,6 +1,5 @@ import { AxiosInstance, AxiosRequestConfig } from 'axios'; import type { Keystore } from './keystore'; -import { BlazeOptions } from '../../blaze/type'; export interface RequestConfig extends Partial< Pick @@ -11,7 +10,6 @@ export interface RequestConfig extends Partial< } export interface HTTPConfig { - blazeOptions?: BlazeOptions; keystore?: Keystore; requestConfig?: RequestConfig; } diff --git a/src/client/types/index.ts b/src/client/types/index.ts index 63d61225..3d74aa08 100644 --- a/src/client/types/index.ts +++ b/src/client/types/index.ts @@ -2,7 +2,6 @@ export * from './address'; export * from './app'; export * from './asset'; export * from './attachment'; -export * from '../../blaze/type'; export * from './circle'; export * from './client'; export * from './code'; diff --git a/src/client/types/message.ts b/src/client/types/message.ts index a4d18c95..c5c470ca 100644 --- a/src/client/types/message.ts +++ b/src/client/types/message.ts @@ -29,23 +29,6 @@ export interface AcknowledgementRequest { status: string; } -export interface MessageView { - type: 'message'; - representative_id: string; - quote_message_id: string; - conversation_id: string; - user_id: string; - session_id: string; - message_id: string; - category: MessageCategory; - data: string; - data_base64: string; - status: MessageStatus; - source: string; - created_at: string; - updated_at: string; -} - export interface MessageRequest { conversation_id: string; recipient_id?: string; diff --git a/src/index.ts b/src/index.ts index d276823d..06b43aa4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,3 @@ export * from './client'; -export * from './blaze'; export * from './webview'; export * from './constant';