/
Offer.ts
66 lines (63 loc) · 2.05 KB
/
Offer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { ClientContext } from "../../ClientContext";
import { Constants, isResourceValid, ResourceType } from "../../common";
import { CosmosClient } from "../../CosmosClient";
import { RequestOptions } from "../../request";
import { OfferDefinition } from "./OfferDefinition";
import { OfferResponse } from "./OfferResponse";
/**
* Use to read or replace an existing {@link Offer} by id.
*
* @see {@link Offers} to query or read all offers.
*/
export class Offer {
/**
* Returns a reference URL to the resource. Used for linking in Permissions.
*/
public get url() {
return `/${Constants.Path.OffersPathSegment}/${this.id}`;
}
/**
* @hidden
* @param client The parent {@link CosmosClient} for the Database Account.
* @param id The id of the given {@link Offer}.
*/
constructor(
public readonly client: CosmosClient,
public readonly id: string,
private readonly clientContext: ClientContext
) {}
/**
* Read the {@link OfferDefinition} for the given {@link Offer}.
* @param options
*/
public async read(options?: RequestOptions): Promise<OfferResponse> {
const response = await this.clientContext.read<OfferDefinition>({
path: this.url,
resourceType: ResourceType.offer,
resourceId: this.id,
options
});
return new OfferResponse(response.result, response.headers, response.code, this);
}
/**
* Replace the given {@link Offer} with the specified {@link OfferDefinition}.
* @param body The specified {@link OfferDefinition}
* @param options
*/
public async replace(body: OfferDefinition, options?: RequestOptions): Promise<OfferResponse> {
const err = {};
if (!isResourceValid(body, err)) {
throw err;
}
const response = await this.clientContext.replace<OfferDefinition>({
body,
path: this.url,
resourceType: ResourceType.offer,
resourceId: this.id,
options
});
return new OfferResponse(response.result, response.headers, response.code, this);
}
}