Skip to content
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,21 @@ 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: '',
session_id: '',
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);
},
Expand Down
15 changes: 6 additions & 9 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,21 @@ 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: '',
session_id: '',
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);
},
Expand Down
13 changes: 6 additions & 7 deletions example/blaze.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -41,4 +40,4 @@ const handler = {
},
};
// ws will auto reconnect after connect closing
client.blaze.loop(handler);
blaze.loop(handler);
4 changes: 3 additions & 1 deletion example/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
25 changes: 0 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 27 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -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",
Expand All @@ -40,23 +58,22 @@
"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": {
"@eslint/js": "^10.0.1",
"@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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down
4 changes: 0 additions & 4 deletions src/blaze/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
35 changes: 32 additions & 3 deletions src/blaze/type.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,7 +42,7 @@ interface TransferView {
created_at: string;
}

interface SystemConversationPayload {
export interface SystemConversationPayload {
action: string;
participant_id: string;
user_id?: string;
Expand Down
4 changes: 2 additions & 2 deletions src/blaze/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 6 additions & 5 deletions src/client/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) => {
Expand Down Expand Up @@ -60,5 +63,3 @@ export function http(keystore?: Keystore, config?: RequestConfig): AxiosInstance

return ins;
}

export const mixinRequest = http();
2 changes: 0 additions & 2 deletions src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
9 changes: 3 additions & 6 deletions src/client/mixin-client.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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),
Expand All @@ -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 };
}
2 changes: 0 additions & 2 deletions src/client/types/client.ts
Original file line number Diff line number Diff line change
@@ -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<AxiosRequestConfig, 'baseURL' | 'headers' | 'timeout' | 'httpAgent' | 'httpsAgent' | 'onDownloadProgress' | 'onUploadProgress' | 'proxy'>
Expand All @@ -11,7 +10,6 @@ export interface RequestConfig extends Partial<
}

export interface HTTPConfig {
blazeOptions?: BlazeOptions;
keystore?: Keystore;
requestConfig?: RequestConfig;
}
Expand Down
Loading
Loading