Skip to content

Commit 2ee9a61

Browse files
author
Andrew Omondi
committed
Setup bnudle for TS
1 parent 1523ec1 commit 2ee9a61

12 files changed

+226
-0
lines changed

eslint.config.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export default [{
5555
parserOptions: {
5656
project: [
5757
"packages/abstractions/tsconfig.json",
58+
"packages/bundle/tsconfig.json",
5859
"packages/authentication/azure/tsconfig.json",
5960
"packages/authentication/azure/tsconfig.json",
6061
"packages/http/fetch/tsconfig.json",

package-lock.json

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"workspaces": [
3131
"packages/test/",
3232
"packages/abstractions",
33+
"packages/bundle",
3334
"packages/http/*",
3435
"packages/serialization/*",
3536
"packages/authentication/*"

packages/bundle/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist
2+
*.tsbuildinfo

packages/bundle/.npmignore

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.eslint**
2+
.prettier**
3+
*.log
4+
*.tgz
5+
**/test/**/*
6+
docs/
7+
node_modules/
8+
rollup.config.js
9+
src/
10+
tsconfig.**.json
11+
vite.config.mts

packages/bundle/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[![npm version badge](https://img.shields.io/npm/v/@microsoft/kiota-bundle?color=blue)](https://www.npmjs.com/package/@microsoft/kiota-bundle)
2+
3+
The Kiota Bundle Library provides default implementations for client setup.
4+
The package provides a request adapter implementation with defaults serialization libraries setup fo use with a generated Kiota client.
5+
6+
Read more about Kiota [here](https://github.com/microsoft/kiota/blob/main/README.md).
7+
8+
## Using the bundle
9+
10+
`npm i @microsoft/kiota-bundle`

packages/bundle/package.json

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"name": "@microsoft/kiota-bundle",
3+
"version": "1.0.0-preview.1",
4+
"description": "Kiota Bundle package providing default implementations for client setup for kiota generated libraries in TypeScript and JavaScript",
5+
"main": "dist/es/src/index.js",
6+
"module": "dist/es/src/index.js",
7+
"types": "dist/es/src/index.d.ts",
8+
"type": "module",
9+
"scripts": {
10+
"build": "npm run build:esm",
11+
"build:esm": "tsc && tsc-alias",
12+
"lint": "eslint . --ext .ts",
13+
"lint:fix": "eslint . --ext .ts --fix",
14+
"clean": "rimraf ./dist",
15+
"test:browser": "vitest run --browser.name=chrome --browser.headless --browser.provider=webdriverio",
16+
"test:node": "vitest --run",
17+
"test": "npm run test:node && npm run test:browser"
18+
},
19+
"repository": "git://github.com/microsoft/kiota-typescript.git",
20+
"keywords": [
21+
"kiota",
22+
"openAPI",
23+
"Microsoft",
24+
"Graph"
25+
],
26+
"author": "Microsoft",
27+
"license": "MIT",
28+
"bugs": {
29+
"url": "https://github.com/microsoft/kiota-typescript/issues"
30+
},
31+
"homepage": "https://github.com/microsoft/kiota#readme",
32+
"devDependencies": {
33+
},
34+
"dependencies": {
35+
"@microsoft/kiota-abstractions": "*",
36+
"@microsoft/kiota-http-fetchlibrary": "*",
37+
"@microsoft/kiota-serialization-form": "*",
38+
"@microsoft/kiota-serialization-json": "*",
39+
"@microsoft/kiota-serialization-text": "*",
40+
"@microsoft/kiota-serialization-multipart": "*"
41+
},
42+
"publishConfig": {
43+
"access": "public"
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* -------------------------------------------------------------------------------------------
3+
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
4+
* See License in the project root for license information.
5+
* -------------------------------------------------------------------------------------------
6+
*/
7+
8+
import {
9+
AuthenticationProvider,
10+
ParseNodeFactory,
11+
ParseNodeFactoryRegistry,
12+
registerDefaultDeserializer,
13+
registerDefaultSerializer,
14+
SerializationWriterFactory,
15+
SerializationWriterFactoryRegistry,
16+
} from "@microsoft/kiota-abstractions";
17+
import { FormParseNodeFactory, FormSerializationWriterFactory } from '@microsoft/kiota-serialization-form';
18+
import { JsonParseNodeFactory, JsonSerializationWriterFactory } from '@microsoft/kiota-serialization-json';
19+
import { MultipartSerializationWriterFactory } from '@microsoft/kiota-serialization-multipart';
20+
import { TextParseNodeFactory, TextSerializationWriterFactory } from '@microsoft/kiota-serialization-text';
21+
import {
22+
FetchRequestAdapter,
23+
HttpClient,
24+
type ObservabilityOptions,
25+
ObservabilityOptionsImpl,
26+
} from "@microsoft/kiota-http-fetchlibrary";
27+
28+
/**
29+
* Default request adapter for graph clients. Bootstraps serialization and other aspects.
30+
*/
31+
export class DefaultRequestAdapter extends FetchRequestAdapter {
32+
/**
33+
* Instantiates a new request adapter.
34+
* @param authenticationProvider the authentication provider to use.
35+
* @param parseNodeFactory the parse node factory to deserialize responses.
36+
* @param serializationWriterFactory the serialization writer factory to use to serialize request bodies.
37+
* @param httpClient the http client to use to execute requests.
38+
* @param observabilityOptions the observability options to use.
39+
*/
40+
public constructor(
41+
authenticationProvider: AuthenticationProvider,
42+
parseNodeFactory: ParseNodeFactory = ParseNodeFactoryRegistry.defaultInstance,
43+
serializationWriterFactory: SerializationWriterFactory = SerializationWriterFactoryRegistry.defaultInstance,
44+
httpClient: HttpClient = new HttpClient(),
45+
observabilityOptions: ObservabilityOptions = new ObservabilityOptionsImpl(),
46+
) {
47+
super(authenticationProvider, parseNodeFactory, serializationWriterFactory, httpClient, observabilityOptions);
48+
DefaultRequestAdapter.setupDefaults();
49+
}
50+
51+
private static setupDefaults() {
52+
registerDefaultSerializer(JsonSerializationWriterFactory);
53+
registerDefaultSerializer(TextSerializationWriterFactory);
54+
registerDefaultSerializer(FormSerializationWriterFactory);
55+
registerDefaultSerializer(MultipartSerializationWriterFactory);
56+
registerDefaultDeserializer(JsonParseNodeFactory);
57+
registerDefaultDeserializer(TextParseNodeFactory);
58+
registerDefaultDeserializer(FormParseNodeFactory);
59+
}
60+
}

packages/bundle/src/index.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* -------------------------------------------------------------------------------------------
3+
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
4+
* See License in the project root for license information.
5+
* -------------------------------------------------------------------------------------------
6+
*/
7+
8+
/* eslint-disable @typescript-eslint/triple-slash-reference*/
9+
10+
export * from "./defaultRequestAdapter";

packages/bundle/test/bundleTests.ts

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* -------------------------------------------------------------------------------------------
3+
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
4+
* See License in the project root for license information.
5+
* -------------------------------------------------------------------------------------------
6+
*/
7+
import { AnonymousAuthenticationProvider, AuthenticationProvider, ParseNodeFactoryRegistry, SerializationWriterFactoryRegistry } from "@microsoft/kiota-abstractions";
8+
import { assert, describe, it } from "vitest";
9+
10+
import { DefaultRequestAdapter } from "../src/defaultRequestAdapter";
11+
12+
13+
describe("defaultRequestAdapter.ts", () => {
14+
describe("Initialization", () => {
15+
it("should throw error on null authProvider", async () => {
16+
assert.throws(() => new DefaultRequestAdapter(null as unknown as AuthenticationProvider), Error, "authentication provider cannot be null");
17+
});
18+
19+
it("should setup serializers correctly", async () => {
20+
const requestAdapter = new DefaultRequestAdapter(new AnonymousAuthenticationProvider());
21+
22+
assert.isNotNull(requestAdapter);
23+
24+
const serializerMap = SerializationWriterFactoryRegistry.defaultInstance.contentTypeAssociatedFactories;
25+
const deserializerMap = ParseNodeFactoryRegistry.defaultInstance.contentTypeAssociatedFactories;
26+
27+
assert.isNotNull(serializerMap);
28+
assert.isNotNull(deserializerMap);
29+
30+
// verify that the default serializers are registered
31+
assert.equal(serializerMap.size, 4);
32+
assert.equal(deserializerMap.size, 3);
33+
34+
// verify that the default serializers are registered by name
35+
assert.isTrue(serializerMap.has("application/json"));
36+
assert.isTrue(serializerMap.has("text/plain"));
37+
assert.isTrue(serializerMap.has("application/x-www-form-urlencoded"));
38+
assert.isTrue(serializerMap.has("multipart/form-data"));
39+
40+
// verify that the default deserializers are registered by name
41+
assert.isTrue(deserializerMap.has("application/json"));
42+
assert.isTrue(deserializerMap.has("text/plain"));
43+
assert.isTrue(deserializerMap.has("application/x-www-form-urlencoded"));
44+
45+
});
46+
});
47+
});

packages/bundle/tsconfig.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"$schema": "https://json.schemastore.org/tsconfig",
3+
"extends": "../../tsconfig.base.json",
4+
"compilerOptions": {
5+
"outDir": "dist/es/"
6+
},
7+
"include": [
8+
"./src/**/*.ts",
9+
]
10+
}

packages/bundle/vite.config.mts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { defineConfig, configDefaults } from "vitest/config";
2+
3+
export default defineConfig({
4+
test: {
5+
exclude: [...configDefaults.exclude, "**/test{Entity,Enum}.ts"],
6+
include: [...configDefaults.include, "test/**/*.ts"],
7+
coverage: {
8+
reporter: ["html"],
9+
},
10+
},
11+
});

0 commit comments

Comments
 (0)