diff --git a/package.json b/package.json index 251bf25..bc5adf2 100644 --- a/package.json +++ b/package.json @@ -2,8 +2,14 @@ "name": "alicloud-tablestore", "type": "module", "version": "0.1.0", + "description": "Alicloud Tablestore SDK for Node.js", "author": "Kevin Cui ", "license": "MIT", + "keywords": [ + "alicloud", + "tablestore", + "ots" + ], "exports": { ".": { "types": "./dist/index.d.mts", diff --git a/src/client.ts b/src/client.ts new file mode 100644 index 0000000..689ce30 --- /dev/null +++ b/src/client.ts @@ -0,0 +1,9 @@ +import type { ClientConfig } from "./type"; +import { Request } from "./request"; + +export class Client { + public request: Request; + public constructor(public readonly config: ClientConfig) { + this.request = new Request(config); + } +} diff --git a/src/index.ts b/src/index.ts index 6b4f22f..309b7e0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,11 @@ +export { + Client, +} from "./client"; + export { OTS_API_NAME, } from "./const"; -export { Basic } from "./operator/basic"; - export { BatchGetRow, type BatchGetRowData, @@ -86,6 +88,7 @@ export { type Filter, FilterType, type LogicalOperator, + OperationType, type ReturnContent, ReturnType, RowExistenceExpectation, @@ -109,9 +112,7 @@ export { export { Request } from "./request"; export { - type OperatorConfig, + type ClientConfig, type OTSApiName, - type RequestConfig, - type RequestFactory, type RequestOptions, } from "./type"; diff --git a/src/operator/basic.ts b/src/operator/basic.ts deleted file mode 100644 index 8c1ef22..0000000 --- a/src/operator/basic.ts +++ /dev/null @@ -1,17 +0,0 @@ -import type { OperatorConfig, RequestFactory } from "../type"; -import { Request } from "../request"; - -export class Basic { - protected request: RequestFactory; - - protected constructor( - config: OperatorConfig, - ) { - if (!config.request) { - this.request = new Request(config); - } - else { - this.request = config.request; - } - } -} diff --git a/src/operator/batch-get-row.ts b/src/operator/batch-get-row.ts index 4d94f9d..eb2e13e 100644 --- a/src/operator/batch-get-row.ts +++ b/src/operator/batch-get-row.ts @@ -1,13 +1,12 @@ +import type { Client } from "../client"; import type { ConsumedCapacity, Error, Filter, TimeRange } from "../pb/type"; import type { PlainBufferCell, PlainBufferRow } from "../plainbuffer"; -import type { OperatorConfig } from "../type"; import { Buffer } from "node:buffer"; import { buildFilter } from "../builder/filter"; import { OTS_API_NAME } from "../const"; import { builder } from "../pb/builder"; import { decodePlainBuffer, encodePlainBuffer } from "../plainbuffer"; import { fixPlainBufferCellType } from "../utils"; -import { Basic } from "./basic"; export const ProtoBatchGetRowRequest = builder.lookupType("ots.BatchGetRowRequest"); export const ProtoBatchGetRowResponse = builder.lookupType("ots.BatchGetRowResponse"); @@ -45,9 +44,8 @@ export interface BatchGetRowResponse { tables: Array; } -export class BatchGetRow extends Basic { - public constructor(config: OperatorConfig) { - super(config); +export class BatchGetRow { + public constructor(private readonly client: Client) { } public static async builder(options: BatchGetRowData) { @@ -97,7 +95,7 @@ export class BatchGetRow extends Basic { public async do(data: BatchGetRowData) { const body = await BatchGetRow.builder(data); - return await this.request.do({ + return await this.client.request.do({ apiName: OTS_API_NAME.BatchGetRow, body, }); diff --git a/src/operator/batch-write-row.ts b/src/operator/batch-write-row.ts index 8f2523c..2d6cda1 100644 --- a/src/operator/batch-write-row.ts +++ b/src/operator/batch-write-row.ts @@ -1,6 +1,6 @@ +import type { Client } from "../client"; import type { Condition, ConsumedCapacity, Error, ReturnContent } from "../pb/type"; import type { PlainBufferCell, PlainBufferRow } from "../plainbuffer"; -import type { OperatorConfig } from "../type"; import type { UpdateRowAttributeColumns } from "./update-row"; import { Buffer } from "node:buffer"; import { buildFilter } from "../builder/filter"; @@ -9,7 +9,6 @@ import { builder } from "../pb/builder"; import { OperationType, RowExistenceExpectation } from "../pb/type"; import { decodePlainBuffer, encodePlainBuffer } from "../plainbuffer"; import { fixPlainBufferCellType } from "../utils"; -import { Basic } from "./basic"; import { UpdateRow } from "./update-row"; export const ProtoBatchWriteRowRequest = builder.lookupType("ots.BatchWriteRowRequest"); @@ -62,9 +61,8 @@ export interface BatchWriteRowResponse { tables: Array; } -export class BatchWriteRow extends Basic { - public constructor(config: OperatorConfig) { - super(config); +export class BatchWriteRow { + public constructor(private readonly client: Client) { } public static async builder(options: BatchWriteRowData) { @@ -111,7 +109,7 @@ export class BatchWriteRow extends Basic { public async do(data: BatchWriteRowData) { const body = await BatchWriteRow.builder(data); - return await this.request.do({ + return await this.client.request.do({ apiName: OTS_API_NAME.BatchWriteRow, body, }); diff --git a/src/operator/delete-row.ts b/src/operator/delete-row.ts index eb63bab..4ac295b 100644 --- a/src/operator/delete-row.ts +++ b/src/operator/delete-row.ts @@ -1,6 +1,6 @@ +import type { Client } from "../client"; import type { Condition, ConsumedCapacity, ReturnContent } from "../pb/type"; import type { PlainBufferCell, PlainBufferRow } from "../plainbuffer"; -import type { OperatorConfig } from "../type"; import { Buffer } from "node:buffer"; import { buildFilter } from "../builder/filter"; import { OTS_API_NAME } from "../const"; @@ -8,7 +8,6 @@ import { builder } from "../pb/builder"; import { RowExistenceExpectation } from "../pb/type"; import { decodePlainBuffer, encodePlainBuffer } from "../plainbuffer"; import { fixPlainBufferCellType } from "../utils"; -import { Basic } from "./basic"; export const ProtoDeleteRowRequest = builder.lookupType("ots.DeleteRowRequest"); export const ProtoDeleteRowResponse = builder.lookupType("ots.DeleteRowResponse"); @@ -26,9 +25,8 @@ export interface DeleteRowResponse { row: Array | null; } -export class DeleteRow extends Basic { - public constructor(config: OperatorConfig) { - super(config); +export class DeleteRow { + public constructor(private readonly client: Client) { } public static async builder(options: DeleteRowData) { @@ -66,7 +64,7 @@ export class DeleteRow extends Basic { public async do(data: DeleteRowData) { const body = await DeleteRow.builder(data); - return await this.request.do({ + return await this.client.request.do({ apiName: OTS_API_NAME.DeleteRow, body, }); diff --git a/src/operator/get-range.ts b/src/operator/get-range.ts index 292639d..2f76e80 100644 --- a/src/operator/get-range.ts +++ b/src/operator/get-range.ts @@ -1,13 +1,12 @@ +import type { Client } from "../client"; import type { ConsumedCapacity, Direction, Filter, TimeRange } from "../pb/type"; import type { PlainBufferCell, PlainBufferRow } from "../plainbuffer"; -import type { OperatorConfig } from "../type"; import { Buffer } from "node:buffer"; import { buildFilter } from "../builder/filter"; import { OTS_API_NAME } from "../const"; import { builder } from "../pb/builder"; import { decodePlainBuffer, encodePlainBuffer } from "../plainbuffer"; import { fixPlainBufferCellType } from "../utils"; -import { Basic } from "./basic"; export const ProtoGetRangeRequest = builder.lookupType("ots.GetRangeRequest"); export const ProtoGetRangeResponse = builder.lookupType("ots.GetRangeResponse"); @@ -32,9 +31,8 @@ export interface GetRangeResponse { nextStartPrimaryKey: PlainBufferRow | null; } -export class GetRange extends Basic { - public constructor(config: OperatorConfig) { - super(config); +export class GetRange { + public constructor(private readonly client: Client) { } public static async builder(options: GetRangeData) { @@ -88,7 +86,7 @@ export class GetRange extends Basic { public async do(data: GetRangeData) { const body = await GetRange.builder(data); - return await this.request.do({ + return await this.client.request.do({ apiName: OTS_API_NAME.GetRange, body, }); diff --git a/src/operator/get-row.ts b/src/operator/get-row.ts index ce347fb..c2588da 100644 --- a/src/operator/get-row.ts +++ b/src/operator/get-row.ts @@ -1,13 +1,12 @@ +import type { Client } from "../client"; import type { ConsumedCapacity, Filter, TimeRange } from "../pb/type"; import type { PlainBufferCell, PlainBufferRow } from "../plainbuffer"; -import type { OperatorConfig } from "../type"; import { Buffer } from "node:buffer"; import { buildFilter } from "../builder/filter"; import { OTS_API_NAME } from "../const"; import { builder } from "../pb/builder"; import { decodePlainBuffer, encodePlainBuffer } from "../plainbuffer"; import { fixPlainBufferCellType } from "../utils"; -import { Basic } from "./basic"; export const ProtoGetRowRequest = builder.lookupType("ots.GetRowRequest"); export const ProtoGetRowResponse = builder.lookupType("ots.GetRowResponse"); @@ -31,9 +30,8 @@ export interface GetRowResponse { nextToken: ArrayBuffer | null; // Tablestore currently not supported } -export class GetRow extends Basic { - public constructor(config: OperatorConfig) { - super(config); +export class GetRow { + public constructor(private readonly client: Client) { } public static async builder(options: GetRowData) { @@ -82,7 +80,7 @@ export class GetRow extends Basic { public async do(data: GetRowData) { const body = await GetRow.builder(data); - return await this.request.do({ + return await this.client.request.do({ apiName: OTS_API_NAME.GetRow, body, }); diff --git a/src/operator/put-row.ts b/src/operator/put-row.ts index eab7842..afa1b0b 100644 --- a/src/operator/put-row.ts +++ b/src/operator/put-row.ts @@ -1,6 +1,6 @@ +import type { Client } from "../client"; import type { Condition, ConsumedCapacity, ReturnContent } from "../pb/type"; import type { PlainBufferCell, PlainBufferRow } from "../plainbuffer"; -import type { OperatorConfig } from "../type"; import { Buffer } from "node:buffer"; import { buildFilter } from "../builder/filter"; import { OTS_API_NAME } from "../const"; @@ -8,7 +8,6 @@ import { builder } from "../pb/builder"; import { RowExistenceExpectation } from "../pb/type"; import { decodePlainBuffer, encodePlainBuffer } from "../plainbuffer"; import { fixPlainBufferCellType } from "../utils"; -import { Basic } from "./basic"; export const ProtoPutRowRequest = builder.lookupType("ots.PutRowRequest"); export const ProtoPutRowResponse = builder.lookupType("ots.PutRowResponse"); @@ -26,9 +25,8 @@ export interface PutRowResponse { consumed: ConsumedCapacity; row: Array | null; } -export class PutRow extends Basic { - public constructor(config: OperatorConfig) { - super(config); +export class PutRow { + public constructor(private readonly client: Client) { } public static async builder(options: PutRowData) { @@ -65,7 +63,7 @@ export class PutRow extends Basic { public async do(data: PutRowData) { const body = await PutRow.builder(data); - return await this.request.do({ + return await this.client.request.do({ apiName: OTS_API_NAME.PutRow, body, }); diff --git a/src/operator/update-row.ts b/src/operator/update-row.ts index 110ffb4..eb9d889 100644 --- a/src/operator/update-row.ts +++ b/src/operator/update-row.ts @@ -1,6 +1,6 @@ +import type { Client } from "../client"; import type { Condition, ConsumedCapacity, ReturnContent } from "../pb/type"; import type { PlainBufferCell, PlainBufferRow } from "../plainbuffer"; -import type { OperatorConfig } from "../type"; import { Buffer } from "node:buffer"; import { buildFilter } from "../builder/filter"; import { OTS_API_NAME } from "../const"; @@ -8,7 +8,6 @@ import { builder } from "../pb/builder"; import { RowExistenceExpectation } from "../pb/type"; import { CellOp, decodePlainBuffer, encodePlainBuffer } from "../plainbuffer"; import { fixPlainBufferCellType } from "../utils"; -import { Basic } from "./basic"; export const ProtoUpdateRowRequest = builder.lookupType("ots.UpdateRowRequest"); export const ProtoUpdateRowResponse = builder.lookupType("ots.UpdateRowResponse"); @@ -33,9 +32,8 @@ export interface UpdateRowResponse { row: Array | null; } -export class UpdateRow extends Basic { - public constructor(config: OperatorConfig) { - super(config); +export class UpdateRow { + public constructor(private readonly client: Client) { } public static async builder(options: UpdateRowData) { @@ -69,7 +67,7 @@ export class UpdateRow extends Basic { public async do(data: UpdateRowData) { const body = await UpdateRow.builder(data); - return await this.request.do({ + return await this.client.request.do({ apiName: OTS_API_NAME.UpdateRow, body, }); diff --git a/src/request.ts b/src/request.ts index 2b397cc..b34731d 100644 --- a/src/request.ts +++ b/src/request.ts @@ -1,4 +1,4 @@ -import type { OTSApiName, RequestConfig, RequestOptions } from "./type"; +import type { ClientConfig, OTSApiName, RequestOptions } from "./type"; import { createHash, createHmac } from "node:crypto"; import ky from "ky"; import { API_VERSION, H_OTS_ACCESS_KEY_ID, H_OTS_API_VERSION, H_OTS_CONTENT_MD5, H_OTS_DATE, H_OTS_INSTANCE_NAME, H_OTS_PREFIX, H_OTS_SIGNATURE, USER_AGENT } from "./const"; @@ -9,7 +9,7 @@ const DEFAULT_REQUEST_OPTIONS = { }; export class Request { - public constructor(private readonly config: RequestConfig) { + public constructor(private readonly config: ClientConfig) { } public async do(options: RequestOptions): Promise { diff --git a/src/type.ts b/src/type.ts index 8c95eb4..24f4d55 100644 --- a/src/type.ts +++ b/src/type.ts @@ -1,6 +1,6 @@ import type { OTS_API_NAME } from "./const"; -export interface RequestConfig { +export interface ClientConfig { endpoint: string; accessKeyID: string; accessKeySecret: string; @@ -13,12 +13,4 @@ export interface RequestOptions { headers?: Record; } -export interface RequestFactory { - do: (options: RequestOptions) => Promise; -} - -export interface OperatorConfig extends RequestConfig { - request?: RequestFactory; -} - export type OTSApiName = keyof typeof OTS_API_NAME;