Skip to content

Commit

Permalink
Generate tokens using @azure/cosmos-sign (v3) (Azure#213)
Browse files Browse the repository at this point in the history
* WIP

* Finish cosmos-sign integration

* Remove create-hmac

* Use enums
  • Loading branch information
southpolesteve authored Jan 3, 2019
1 parent bf6b04f commit 64e4d83
Show file tree
Hide file tree
Showing 27 changed files with 346 additions and 242 deletions.
83 changes: 13 additions & 70 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@
"typescript": "3.2.2"
},
"dependencies": {
"@azure/cosmos-sign": "1.0.2",
"binary-search-bounds": "2.0.3",
"create-hmac": "^1.1.7",
"priorityqueuejs": "1.0.0",
"semaphore": "1.0.5",
"stream-http": "^2.8.3",
Expand Down
50 changes: 26 additions & 24 deletions src/ClientContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { Resource } from "./client/Resource";
import {
getIdFromLink,
getPathFromLink,
HTTPMethod,
parseConnectionPolicy,
parseLink,
ResourceType,
setIsUpsertHeader,
StatusCodes,
SubStatusCodes
Expand Down Expand Up @@ -53,7 +55,7 @@ export class ClientContext {
/** @ignore */
public async read<T>(
path: string,
type: string,
type: ResourceType,
id: string,
initialHeaders: IHeaders,
options?: RequestOptions
Expand All @@ -62,7 +64,7 @@ export class ClientContext {
const requestHeaders = await getHeaders(
this.cosmosClientOptions.auth,
{ ...initialHeaders, ...this.cosmosClientOptions.defaultHeaders, ...(options && options.initialHeaders) },
"get",
HTTPMethod.get,
path,
id,
type,
Expand Down Expand Up @@ -92,7 +94,7 @@ export class ClientContext {

public async queryFeed<T>(
path: string,
type: string, // TODO: code smell: enum?
type: ResourceType,
id: string,
resultFn: (result: { [key: string]: any }) => any[], // TODO: any
query: SqlQuerySpec | string,
Expand All @@ -117,7 +119,7 @@ export class ClientContext {
const reqHeaders = await getHeaders(
this.cosmosClientOptions.auth,
initialHeaders,
"get",
HTTPMethod.get,
path,
id,
type,
Expand Down Expand Up @@ -149,7 +151,7 @@ export class ClientContext {
const reqHeaders = await getHeaders(
this.cosmosClientOptions.auth,
initialHeaders,
"post",
HTTPMethod.post,
path,
id,
type,
Expand All @@ -167,17 +169,17 @@ export class ClientContext {
}

public queryPartitionKeyRanges(collectionLink: string, query?: string | SqlQuerySpec, options?: FeedOptions) {
const path = getPathFromLink(collectionLink, "pkranges");
const path = getPathFromLink(collectionLink, ResourceType.pkranges);
const id = getIdFromLink(collectionLink);
const cb: FetchFunctionCallback = innerOptions => {
return this.queryFeed(path, "pkranges", id, result => result.PartitionKeyRanges, query, innerOptions);
return this.queryFeed(path, ResourceType.pkranges, id, result => result.PartitionKeyRanges, query, innerOptions);
};
return new QueryIterator<PartitionKeyRange>(this, query, options, cb);
}

public async delete<T>(
path: string,
type: string,
type: ResourceType,
id: string,
initialHeaders: IHeaders,
options?: RequestOptions
Expand All @@ -186,7 +188,7 @@ export class ClientContext {
const reqHeaders = await getHeaders(
this.cosmosClientOptions.auth,
{ ...initialHeaders, ...this.cosmosClientOptions.defaultHeaders, ...(options && options.initialHeaders) },
"delete",
HTTPMethod.delete,
path,
id,
type,
Expand Down Expand Up @@ -222,7 +224,7 @@ export class ClientContext {
public async create<T>(
body: T,
path: string,
type: string,
type: ResourceType,
id: string,
initialHeaders: IHeaders,
options?: RequestOptions
Expand All @@ -232,15 +234,15 @@ export class ClientContext {
public async create<T, U>(
body: T,
path: string,
type: string,
type: ResourceType,
id: string,
initialHeaders: IHeaders,
options?: RequestOptions
): Promise<Response<T & U & Resource>>;
public async create<T, U>(
body: T,
path: string,
type: string,
type: ResourceType,
id: string,
initialHeaders: IHeaders,
options?: RequestOptions
Expand All @@ -249,7 +251,7 @@ export class ClientContext {
const requestHeaders = await getHeaders(
this.cosmosClientOptions.auth,
{ ...initialHeaders, ...this.cosmosClientOptions.defaultHeaders, ...(options && options.initialHeaders) },
"post",
HTTPMethod.post,
path,
id,
type,
Expand Down Expand Up @@ -318,7 +320,7 @@ export class ClientContext {
public async replace<T>(
resource: any,
path: string,
type: string,
type: ResourceType,
id: string,
initialHeaders: IHeaders,
options?: RequestOptions
Expand All @@ -327,7 +329,7 @@ export class ClientContext {
const reqHeaders = await getHeaders(
this.cosmosClientOptions.auth,
{ ...initialHeaders, ...this.cosmosClientOptions.defaultHeaders, ...(options && options.initialHeaders) },
"put",
HTTPMethod.put,
path,
id,
type,
Expand Down Expand Up @@ -359,23 +361,23 @@ export class ClientContext {
public async upsert<T>(
body: T,
path: string,
type: string,
type: ResourceType,
id: string,
initialHeaders: IHeaders,
options?: RequestOptions
): Promise<Response<T & Resource>>;
public async upsert<T, U>(
body: T,
path: string,
type: string,
type: ResourceType,
id: string,
initialHeaders: IHeaders,
options?: RequestOptions
): Promise<Response<T & U & Resource>>;
public async upsert<T>(
body: T,
path: string,
type: string,
type: ResourceType,
id: string,
initialHeaders: IHeaders,
options?: RequestOptions
Expand All @@ -384,7 +386,7 @@ export class ClientContext {
const requestHeaders = await getHeaders(
this.cosmosClientOptions.auth,
{ ...initialHeaders, ...this.cosmosClientOptions.defaultHeaders, ...(options && options.initialHeaders) },
"post",
HTTPMethod.post,
path,
id,
type,
Expand Down Expand Up @@ -432,10 +434,10 @@ export class ClientContext {
const headers = await getHeaders(
this.cosmosClientOptions.auth,
initialHeaders,
"post",
HTTPMethod.post,
path,
id,
"sprocs",
ResourceType.sproc,
options,
undefined,
this.cosmosClientOptions.connectionPolicy.UseMultipleWriteLocations
Expand All @@ -445,7 +447,7 @@ export class ClientContext {
client: this,
operationType: Constants.OperationTypes.Execute,
path,
resourceType: "sprocs"
resourceType: ResourceType.sproc
};

// executeStoredProcedure will use WriteEndpoint since it uses POST operation
Expand All @@ -464,10 +466,10 @@ export class ClientContext {
const requestHeaders = await getHeaders(
this.cosmosClientOptions.auth,
this.cosmosClientOptions.defaultHeaders,
"get",
"",
HTTPMethod.get,
"",
"",
ResourceType.none,
{},
undefined,
this.cosmosClientOptions.connectionPolicy.UseMultipleWriteLocations
Expand Down
Loading

0 comments on commit 64e4d83

Please sign in to comment.