Skip to content
This repository has been archived by the owner on Aug 18, 2024. It is now read-only.

Commit

Permalink
Update dependency @microsoft/kiota-http-fetchlibrary to v1.0.0-previe…
Browse files Browse the repository at this point in the history
…w.48 (#30)

* Update dependency @microsoft/kiota-http-fetchlibrary to v1.0.0-preview.48

* Fix bug when upgrading `kiota-http-fetchlibrary`

* Refactor test for `NestRequestAdapter`

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: yasaichi <[email protected]>
  • Loading branch information
renovate[bot] and yasaichi authored Mar 24, 2024
1 parent b9d8eef commit 4dd1929
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 39 deletions.
138 changes: 119 additions & 19 deletions libs/nestjs-kiota/src/NestRequestAdapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@ describe(NestRequestAdapter.name, () => {
additionalData: {},
responseHeaders: {},
responseStatusCode: 404,
bar: 'bar',
foo: 42,
};
const baseUrl = 'https://example.com';
const defaultApiError = new DefaultApiError('unexpected error type');
defaultApiError.responseStatusCode = 401;
const primitiveResponseModel = 42;
const responseModel = { foo: 'foo' };

beforeEach(() => {
// TODO: Stop using FetchRequestAdapter
Expand All @@ -40,20 +37,25 @@ describe(NestRequestAdapter.name, () => {
});

describe('baseUrl', () => {
it('should return the same `baseUrl` value as the wrapped adapter has', () => {
requestAdapter.baseUrl = baseUrl;
assert.strictEqual(nestRequestAdapter.baseUrl, baseUrl);
const baseUrl = 'https://example.com';

describe('getter', () => {
it('should return the same value as the wrapped adapter has', () => {
requestAdapter.baseUrl = baseUrl;
assert.strictEqual(nestRequestAdapter.baseUrl, baseUrl);
});
});
});

describe('baseUrl=', () => {
it('should change the `baseUrl` value of the wrapped adapter', () => {
nestRequestAdapter.baseUrl = baseUrl;
assert.strictEqual(requestAdapter.baseUrl, baseUrl);
describe('setter', () => {
it('should change the value of the wrapped adapter', () => {
nestRequestAdapter.baseUrl = baseUrl;
assert.strictEqual(requestAdapter.baseUrl, baseUrl);
});
});
});

describe('send', () => {
const responseModel = { foo: 42 };
const args = [
new RequestInformation(HttpMethod.GET),
fake(),
Expand Down Expand Up @@ -83,7 +85,7 @@ describe(NestRequestAdapter.name, () => {

assert(error instanceof HttpException);
assert.strictEqual(error.getStatus(), apiError.responseStatusCode);
assert.deepEqual(error.getResponse(), { bar: apiError.bar });
assert.deepEqual(error.getResponse(), { foo: apiError.foo });

return true;
},
Expand All @@ -92,7 +94,8 @@ describe(NestRequestAdapter.name, () => {
});
});

describe('sendPrimitive', () => {
describe(NestRequestAdapter.prototype.sendPrimitive.name, () => {
const primitiveResponseModel = 42;
const args = [
new RequestInformation(HttpMethod.GET),
'number',
Expand Down Expand Up @@ -139,7 +142,8 @@ describe(NestRequestAdapter.name, () => {
});
});

describe('sendCollection', () => {
describe(NestRequestAdapter.prototype.sendCollection.name, () => {
const responseModel = { foo: 42 };
const args = [
new RequestInformation(HttpMethod.GET),
fake(),
Expand Down Expand Up @@ -172,7 +176,7 @@ describe(NestRequestAdapter.name, () => {

assert(error instanceof HttpException);
assert.strictEqual(error.getStatus(), apiError.responseStatusCode);
assert.deepEqual(error.getResponse(), { bar: apiError.bar });
assert.deepEqual(error.getResponse(), { foo: apiError.foo });

return true;
},
Expand All @@ -181,7 +185,8 @@ describe(NestRequestAdapter.name, () => {
});
});

describe('sendCollectionOfPrimitive', () => {
describe(NestRequestAdapter.prototype.sendCollectionOfPrimitive.name, () => {
const primitiveResponseModel = 42;
const args = [
new RequestInformation(HttpMethod.GET),
'number',
Expand Down Expand Up @@ -232,7 +237,7 @@ describe(NestRequestAdapter.name, () => {
});
});

describe('sendNoResponseContent', () => {
describe(NestRequestAdapter.prototype.sendNoResponseContent.name, () => {
const args = [
new RequestInformation(HttpMethod.DELETE),
undefined,
Expand Down Expand Up @@ -266,7 +271,102 @@ describe(NestRequestAdapter.name, () => {

assert(error instanceof HttpException);
assert.strictEqual(error.getStatus(), apiError.responseStatusCode);
assert.deepEqual(error.getResponse(), { bar: apiError.bar });
assert.deepEqual(error.getResponse(), { foo: apiError.foo });

return true;
},
);
});
});
});

describe(NestRequestAdapter.prototype.sendEnum.name, () => {
const responseModel = { foo: 42 };
const args = [
new RequestInformation(HttpMethod.GET),
responseModel,
undefined,
] as const;

describe('when the wrapped method call succeeds', () => {
beforeEach(() => {
requestAdapter.sendEnum.returns(Promise.resolve(responseModel.foo));
});

it('should return a response model the wrapped method returns', async () => {
assert.strictEqual(
await nestRequestAdapter.sendEnum(...args),
responseModel.foo,
);
});
});

describe('when the wrapped method call fails', () => {
beforeEach(() => {
requestAdapter.sendEnum.throwsException(defaultApiError);
});

it('should throw `HttpException`', () => {
assert.rejects(
nestRequestAdapter.sendEnum(...args),
(error) => {
assert(
requestAdapter.sendEnum.calledOnceWith(...args),
);

assert(error instanceof HttpException);
assert.strictEqual(
error.getStatus(),
defaultApiError.responseStatusCode,
);
assert.deepEqual(error.getResponse(), defaultApiError.message);

return true;
},
);
});
});
});

describe(NestRequestAdapter.prototype.sendCollectionOfEnum.name, () => {
const responseModel = { foo: 42 };
const args = [
new RequestInformation(HttpMethod.GET),
responseModel,
undefined,
] as const;

describe('when the wrapped method call succeeds', () => {
beforeEach(() => {
requestAdapter.sendCollectionOfEnum.returns(
Promise.resolve([responseModel.foo]),
);
});

it('should return a response model collection the wrapped method returns', async () => {
assert.deepEqual(
await nestRequestAdapter.sendCollectionOfEnum(...args),
[responseModel.foo],
);
});
});

describe('when the wrapped method call fails', () => {
beforeEach(() => {
requestAdapter.sendCollectionOfEnum.throwsException(apiError);
});

it('should throw `HttpException`', () => {
assert.rejects(
nestRequestAdapter.sendCollectionOfEnum(...args),
(error) => {
assert(
requestAdapter.sendCollectionOfEnum.calledOnceWith(...args),
);

assert(error instanceof HttpException);
assert.strictEqual(error.getStatus(), apiError.responseStatusCode);
assert.deepEqual(error.getResponse(), { foo: apiError.foo });

return true;
},
Expand Down
30 changes: 29 additions & 1 deletion libs/nestjs-kiota/src/NestRequestAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type { SetNonNullable } from 'type-fest';
// https://github.com/microsoft/kiota-typescript/blob/5ce4f291d27a8142a6df0716a38e06a765fb6563/packages/http/fetch/src/fetchRequestAdapter.ts#L343-L357
export type DeserializedApiError =
& AdditionalDataHolder
& SetNonNullable<Pick<ApiError, 'responseHeaders' | 'responseStatusCode'>>
& SetNonNullable<Omit<ApiError, keyof Error>>
& Record<string, unknown>;

export class NestRequestAdapter implements RequestAdapter {
Expand Down Expand Up @@ -113,6 +113,34 @@ export class NestRequestAdapter implements RequestAdapter {
);
}

sendEnum<EnumObject extends Record<string, unknown>>(
requestInfo: RequestInformation,
enumObject: EnumObject,
errorMappings: ErrorMappings | undefined,
) {
return this.catchApiError(() =>
this.requestAdapter.sendEnum(
requestInfo,
enumObject,
errorMappings,
)
);
}

sendCollectionOfEnum<EnumObject extends Record<string, unknown>>(
requestInfo: RequestInformation,
enumObject: EnumObject,
errorMappings: ErrorMappings | undefined,
) {
return this.catchApiError(() =>
this.requestAdapter.sendCollectionOfEnum(
requestInfo,
enumObject,
errorMappings,
)
);
}

enableBackingStore(
backingStoreFactory?: BackingStoreFactory | undefined,
) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
"typecheck": "deno check src/main.ts"
},
"dependencies": {
"@microsoft/kiota-abstractions": "1.0.0-preview.48",
"@microsoft/kiota-http-fetchlibrary": "1.0.0-preview.47",
"@microsoft/kiota-abstractions": "1.0.0-preview.49",
"@microsoft/kiota-http-fetchlibrary": "1.0.0-preview.48",
"@microsoft/kiota-serialization-form": "1.0.0-preview.38",
"@microsoft/kiota-serialization-json": "1.0.0-preview.49",
"@microsoft/kiota-serialization-multipart": "1.0.0-preview.28",
Expand Down
23 changes: 6 additions & 17 deletions pnpm-lock.yaml

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

0 comments on commit 4dd1929

Please sign in to comment.