Skip to content

Commit 5245f7e

Browse files
authored
[Tables] Create clients from connectionString (Azure#10292)
* Initial changes to support SharedKey auth * Add test for utility helpers * re-enable pipeline validation on PR * Add javascript samples * update sample require * Address PR comments * Add url to externalNodeBuiltins * Support SharedKeyCredential * Rename ShareKeyCredential to TablesSharedKeyCredential * Address PR comments * Fix file name TablesShredKeyCredential
1 parent 1645bf7 commit 5245f7e

29 files changed

+1110
-18
lines changed

sdk/tables/azure-tables/package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
"sdk-type": "client",
66
"main": "dist/index.js",
77
"module": "dist-esm/src/index.js",
8+
"browser": {
9+
"./dist-esm/src/TablesSharedKeyCredential.js": "./dist-esm/src/TablesSharedKeyCredential.browser.js",
10+
"./dist-esm/src/utils/fromAccountConnectionString.js": "./dist-esm/src/utils/fromAccountConnectionString.browser.js",
11+
"./dist-esm/src/utils/url.js": "./dist-esm/src/utils/url.browser.js"
12+
},
813
"types": "types/latest/tables.d.ts",
914
"scripts": {
1015
"audit": "node ../../../common/scripts/rush-audit.js && rimraf node_modules package-lock.json && npm i --package-lock-only 2>&1 && npm audit",
@@ -30,8 +35,8 @@
3035
"test:browser": "npm run build:test && npm run unit-test:browser && npm run integration-test:browser",
3136
"test:node": "npm run build:test && npm run unit-test:node && npm run integration-test:node",
3237
"test": "npm run build:test && npm run unit-test && npm run integration-test",
33-
"unit-test:browser": "echo skipped",
34-
"unit-test:node": "echo skipped",
38+
"unit-test:browser": "karma start --single-run",
39+
"unit-test:node": "mocha --reporter ../../../common/tools/mocha-multi-reporter.js dist-test/index.node.js",
3540
"unit-test": "npm run unit-test:node && npm run unit-test:browser"
3641
},
3742
"files": [
@@ -117,4 +122,4 @@
117122
"typescript": "~3.9.3",
118123
"util": "^0.12.1"
119124
}
120-
}
125+
}

sdk/tables/azure-tables/review/tables.api.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44
55
```ts
66

7+
import { BaseRequestPolicy } from '@azure/core-http';
78
import * as coreHttp from '@azure/core-http';
9+
import { HttpOperationResponse } from '@azure/core-http';
10+
import { RequestPolicy } from '@azure/core-http';
11+
import { RequestPolicyFactory } from '@azure/core-http';
12+
import { RequestPolicyOptions } from '@azure/core-http';
13+
import { WebResource } from '@azure/core-http';
814

915
// @public
1016
export interface AccessPolicy {
@@ -289,9 +295,11 @@ export interface SignedIdentifier {
289295

290296
// @public
291297
export class TableClient {
298+
constructor(url: string, tableName: string, credential: TablesSharedKeyCredential, options?: TableServiceClientOptions);
292299
constructor(url: string, tableName: string, options?: TableServiceClientOptions);
293300
createEntity(entity?: Entity, options?: CreateEntityOptions): Promise<CreateEntityResponse>;
294301
deleteEntity(partitionKey: string, rowKey: string, ifMatch: string, options?: DeleteEntityOptions): Promise<DeleteEntityResponse>;
302+
static fromConnectionString(connectionString: string, tableName: string, options?: TableServiceClientOptions): TableClient;
295303
getAccessPolicy(options?: GetAccessPolicyOptions): Promise<GetAccessPolicyResponse>;
296304
getEntity(partitionKey: string, rowKey: string, options?: GetEntityOptions): Promise<GetEntityResponse>;
297305
listEntities(query?: QueryOptions, options?: ListEntitiesOptions): Promise<ListEntitiesResponse>;
@@ -471,11 +479,13 @@ export interface TableResponseProperties {
471479

472480
// @public
473481
export class TableServiceClient {
482+
constructor(url: string, credential: TablesSharedKeyCredential, options?: TableServiceClientOptions);
474483
constructor(url: string, options?: TableServiceClientOptions);
475484
createEntity(tableName: string, entity?: Entity, options?: CreateEntityOptions): Promise<CreateEntityResponse>;
476485
createTable(tableName: string, options?: CreateTableOptions): Promise<CreateTableResponse>;
477486
deleteEntity(tableName: string, partitionKey: string, rowKey: string, ifMatch: string, options?: DeleteEntityOptions): Promise<DeleteEntityResponse>;
478487
deleteTable(tableName: string, options?: DeleteTableOptions): Promise<DeleteTableResponse>;
488+
static fromConnectionString(connectionString: string, options?: TableServiceClientOptions): TableServiceClient;
479489
getAccessPolicy(tableName: string, options?: GetAccessPolicyOptions): Promise<GetAccessPolicyResponse>;
480490
getEntity(tableName: string, partitionKey: string, rowKey: string, options?: GetEntityOptions): Promise<GetEntityResponse>;
481491
getProperties(options?: GetPropertiesOptions): Promise<GetPropertiesResponse>;
@@ -514,6 +524,21 @@ export interface TableSetAccessPolicyOptionalParams extends coreHttp.OperationOp
514524
timeout?: number;
515525
}
516526

527+
// @public
528+
export class TablesSharedKeyCredential implements RequestPolicyFactory {
529+
constructor(accountName: string, accountKey: string);
530+
readonly accountName: string;
531+
computeHMACSHA256(stringToSign: string): string;
532+
create(nextPolicy: RequestPolicy, options: RequestPolicyOptions): TablesSharedKeyCredentialPolicy;
533+
}
534+
535+
// @public
536+
export class TablesSharedKeyCredentialPolicy extends BaseRequestPolicy {
537+
constructor(nextPolicy: RequestPolicy, options: RequestPolicyOptions, factory: TablesSharedKeyCredential);
538+
sendRequest(request: WebResource): Promise<HttpOperationResponse>;
539+
protected signRequest(request: WebResource): WebResource;
540+
}
541+
517542
// @public
518543
export interface TableUpdateEntityHeaders {
519544
clientRequestId?: string;

sdk/tables/azure-tables/rollup.base.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const input = "dist-esm/src/index.js";
1414
const production = process.env.NODE_ENV === "production";
1515

1616
export function nodeConfig(test = false) {
17-
const externalNodeBuiltins = [];
17+
const externalNodeBuiltins = ["crypto", "url"];
1818
const baseConfig = {
1919
input: input,
2020
external: depNames.concat(externalNodeBuiltins),
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "azure-tables-samples-js",
3+
"private": true,
4+
"version": "0.1.0",
5+
"description": "Azure Tables client library samples for Javascript",
6+
"engine": {
7+
"node": ">=8.0.0"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/Azure/azure-sdk-for-js.git"
12+
},
13+
"keywords": [
14+
"Azure",
15+
"Storage",
16+
"Tables",
17+
"Node.js"
18+
],
19+
"author": "Microsoft Corporation",
20+
"license": "MIT",
21+
"bugs": {
22+
"url": "https://github.com/Azure/azure-sdk-for-js/issues"
23+
},
24+
"homepage": "https://github.com/Azure/azure-sdk-for-js#readme",
25+
"sideEffects": false,
26+
"dependencies": {
27+
"@azure/tables": "../..",
28+
"dotenv": "^8.2.0"
29+
},
30+
"devDependencies": {
31+
"@types/node": "^8.0.0"
32+
}
33+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
const { TableServiceClient, TableClient } = require("@azure/tables");
5+
6+
// Load the .env file if it exists
7+
const dotenv = require("dotenv");
8+
dotenv.config();
9+
10+
const connectionString = process.env["ACCOUNT_CONNECTION_STRING"] || "";
11+
// const connectionString = process.env["SAS_CONNECTION_STRING"] || "";
12+
13+
async function listTables() {
14+
const client = TableServiceClient.fromConnectionString(connectionString);
15+
16+
const tables = await client.listTables();
17+
18+
console.log(tables.value);
19+
}
20+
21+
async function listEntities() {
22+
const client = TableClient.fromConnectionString(connectionString, "test1");
23+
24+
const entities = await client.listEntities();
25+
26+
console.log(entities.value);
27+
}
28+
29+
async function main() {
30+
await listTables();
31+
await listEntities();
32+
}
33+
34+
main().catch(console.error);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
const { TableServiceClient, TableClient } = require("@azure/tables");
5+
6+
// Load the .env file if it exists
7+
const dotenv = require("dotenv");
8+
dotenv.config();
9+
10+
const accountSas = process.env["ACCOUNT_SAS"] || "";
11+
const accountName = process.env["ACCOUNT_NAME"] || "";
12+
13+
async function listTables() {
14+
const accountUrl = `https://${accountName}.table.core.windows.net${accountSas}`;
15+
const client = new TableServiceClient(accountUrl);
16+
17+
const tables = await client.listTables();
18+
19+
console.log(tables.value);
20+
}
21+
22+
async function listEntities() {
23+
const accountUrl = `https://${accountName}.table.core.windows.net${accountSas}`;
24+
const tableName = "test1";
25+
const client = new TableClient(accountUrl, tableName);
26+
27+
const entities = await client.listEntities();
28+
29+
console.log(entities.value);
30+
}
31+
32+
async function main() {
33+
await listTables();
34+
await listEntities();
35+
}
36+
37+
main().catch(console.error);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
const { TableServiceClient, TableClient, TablesSharedKeyCredential } = require("@azure/tables");
5+
6+
// Load the .env file if it exists
7+
const dotenv = require("dotenv");
8+
dotenv.config();
9+
10+
const accountName = process.env["ACCOUNT_NAME"] || "";
11+
const accountKey = process.env["ACCOUNT_KEY"] || "";
12+
const accountUrl = process.env["ACCOUNT_URL"] || "";
13+
14+
async function listTables() {
15+
const credential = new TablesSharedKeyCredential(accountName, accountKey);
16+
const client = new TableServiceClient(accountUrl, credential);
17+
18+
const tables = await client.listTables();
19+
20+
console.log(tables.value);
21+
}
22+
23+
async function listEntities() {
24+
const tableName = "test1";
25+
const credential = new TablesSharedKeyCredential(accountName, accountKey);
26+
const client = new TableClient(accountUrl, tableName, credential);
27+
28+
const entities = await client.listEntities();
29+
30+
console.log(entities.value);
31+
}
32+
33+
async function main() {
34+
await listTables();
35+
await listEntities();
36+
}
37+
38+
main().catch(console.error);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
ACCOUNT_CONNECTION_STRING=
2+
SAS_CONNECTION_STRING=
3+
ACCOUNT_NAME=
4+
ACCOUNT_KEY=
5+
ACCOUNT_URL=
6+
ACCOUNT_SAS=
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "azure-tables-samples-ts",
3+
"private": true,
4+
"version": "0.1.0",
5+
"description": "Azure Tables client library samples for TypeScript",
6+
"engine": {
7+
"node": ">=8.0.0"
8+
},
9+
"scripts": {
10+
"build": "tsc",
11+
"prebuild": "rimraf dist/"
12+
},
13+
"repository": {
14+
"type": "git",
15+
"url": "git+https://github.com/Azure/azure-sdk-for-js.git"
16+
},
17+
"keywords": [
18+
"Azure",
19+
"Storage",
20+
"Tables",
21+
"Node.js",
22+
"TypeScript"
23+
],
24+
"author": "Microsoft Corporation",
25+
"license": "MIT",
26+
"bugs": {
27+
"url": "https://github.com/Azure/azure-sdk-for-js/issues"
28+
},
29+
"homepage": "https://github.com/Azure/azure-sdk-for-js#readme",
30+
"sideEffects": false,
31+
"dependencies": {
32+
"@azure/tables": "../..",
33+
"dotenv": "^8.2.0"
34+
},
35+
"devDependencies": {
36+
"@types/node": "^8.0.0",
37+
"rimraf": "^3.0.0",
38+
"typescript": "~3.6.4"
39+
}
40+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
import { TableServiceClient, TableClient } from "@azure/tables";
5+
6+
// Load the .env file if it exists
7+
import * as dotenv from "dotenv";
8+
dotenv.config();
9+
10+
const connectionString = process.env["ACCOUNT_CONNECTION_STRING"] || "";
11+
// const connectionString = process.env["SAS_CONNECTION_STRING"] || "";
12+
async function listTables() {
13+
const client = TableServiceClient.fromConnectionString(connectionString);
14+
15+
const tables = await client.listTables();
16+
17+
console.log(tables.value);
18+
}
19+
20+
async function listEntities() {
21+
const client = TableClient.fromConnectionString(connectionString, "test1");
22+
23+
const entities = await client.listEntities();
24+
25+
console.log(entities.value);
26+
}
27+
28+
async function main() {
29+
await listTables();
30+
await listEntities();
31+
}
32+
33+
main().catch(console.error);

0 commit comments

Comments
 (0)