diff --git a/eslint.config.mjs b/eslint.config.mjs index 8c1ef07be..764dd3434 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -55,6 +55,7 @@ export default [{ parserOptions: { project: [ "packages/abstractions/tsconfig.json", + "packages/bundle/tsconfig.json", "packages/authentication/azure/tsconfig.json", "packages/authentication/azure/tsconfig.json", "packages/http/fetch/tsconfig.json", diff --git a/lerna.json b/lerna.json index 8e5a05c5b..70c5eac87 100644 --- a/lerna.json +++ b/lerna.json @@ -2,6 +2,7 @@ "packages": [ "packages/test", "packages/abstractions", + "packages/bundle", "packages/http/*", "packages/serialization/*", "packages/authentication/*" diff --git a/package-lock.json b/package-lock.json index 9ed1e4f71..cb12c683f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,7 @@ "workspaces": [ "packages/test/", "packages/abstractions", + "packages/bundle", "packages/http/*", "packages/serialization/*", "packages/authentication/*" @@ -1735,6 +1736,10 @@ "resolved": "packages/authentication/spfx", "link": true }, + "node_modules/@microsoft/kiota-bundle": { + "resolved": "packages/bundle", + "link": true + }, "node_modules/@microsoft/kiota-http-fetchlibrary": { "resolved": "packages/http/fetch", "link": true @@ -15876,6 +15881,19 @@ "tslib": "^2.6.2" } }, + "packages/bundle": { + "version": "1.0.0-preview.1", + "license": "MIT", + "dependencies": { + "@microsoft/kiota-abstractions": "*", + "@microsoft/kiota-http-fetchlibrary": "*", + "@microsoft/kiota-serialization-form": "*", + "@microsoft/kiota-serialization-json": "*", + "@microsoft/kiota-serialization-multipart": "*", + "@microsoft/kiota-serialization-text": "*" + }, + "devDependencies": {} + }, "packages/http/fetch": { "name": "@microsoft/kiota-http-fetchlibrary", "version": "1.0.0-preview.56", diff --git a/package.json b/package.json index b4149b5b8..89c48278e 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "workspaces": [ "packages/test/", "packages/abstractions", + "packages/bundle", "packages/http/*", "packages/serialization/*", "packages/authentication/*" diff --git a/packages/abstractions/package.json b/packages/abstractions/package.json index 147774640..e0b0da6d2 100644 --- a/packages/abstractions/package.json +++ b/packages/abstractions/package.json @@ -1,6 +1,6 @@ { "name": "@microsoft/kiota-abstractions", - "version": "1.0.0-preview.57", + "version": "1.0.0-preview.58", "description": "Core abstractions for kiota generated libraries in TypeScript and JavaScript", "main": "dist/es/src/index.js", "module": "dist/es/src/index.js", diff --git a/packages/authentication/azure/package.json b/packages/authentication/azure/package.json index 2f34e43f4..ce1c504cf 100644 --- a/packages/authentication/azure/package.json +++ b/packages/authentication/azure/package.json @@ -1,6 +1,6 @@ { "name": "@microsoft/kiota-authentication-azure", - "version": "1.0.0-preview.52", + "version": "1.0.0-preview.53", "description": "Authentication provider for Kiota using Azure Identity", "main": "dist/es/src/index.js", "module": "dist/es/src/index.js", diff --git a/packages/authentication/spfx/package.json b/packages/authentication/spfx/package.json index e1af95739..85670b3a9 100644 --- a/packages/authentication/spfx/package.json +++ b/packages/authentication/spfx/package.json @@ -1,6 +1,6 @@ { "name": "@microsoft/kiota-authentication-spfx", - "version": "1.0.0-preview.46", + "version": "1.0.0-preview.47", "description": "Authentication provider for using Kiota in SPFx solutions", "main": "dist/es/src/index.js", "module": "dist/es/src/index.js", diff --git a/packages/bundle/.gitignore b/packages/bundle/.gitignore new file mode 100644 index 000000000..75f7ba5bd --- /dev/null +++ b/packages/bundle/.gitignore @@ -0,0 +1,2 @@ +dist +*.tsbuildinfo \ No newline at end of file diff --git a/packages/bundle/.npmignore b/packages/bundle/.npmignore new file mode 100644 index 000000000..1f066a783 --- /dev/null +++ b/packages/bundle/.npmignore @@ -0,0 +1,11 @@ +.eslint** +.prettier** +*.log +*.tgz +**/test/**/* +docs/ +node_modules/ +rollup.config.js +src/ +tsconfig.**.json +vite.config.mts diff --git a/packages/bundle/README.md b/packages/bundle/README.md new file mode 100644 index 000000000..58c4aa5b3 --- /dev/null +++ b/packages/bundle/README.md @@ -0,0 +1,10 @@ +[![npm version badge](https://img.shields.io/npm/v/@microsoft/kiota-bundle?color=blue)](https://www.npmjs.com/package/@microsoft/kiota-bundle) + +The Kiota Bundle Library provides default implementations for client setup. +The package provides a request adapter implementation with defaults serialization libraries setup fo use with a generated Kiota client. + +Read more about Kiota [here](https://github.com/microsoft/kiota/blob/main/README.md). + +## Using the bundle + +`npm i @microsoft/kiota-bundle` diff --git a/packages/bundle/package.json b/packages/bundle/package.json new file mode 100644 index 000000000..19444e9de --- /dev/null +++ b/packages/bundle/package.json @@ -0,0 +1,45 @@ +{ + "name": "@microsoft/kiota-bundle", + "version": "1.0.0-preview.1", + "description": "Kiota Bundle package providing default implementations for client setup for kiota generated libraries in TypeScript and JavaScript", + "main": "dist/es/src/index.js", + "module": "dist/es/src/index.js", + "types": "dist/es/src/index.d.ts", + "type": "module", + "scripts": { + "build": "npm run build:esm", + "build:esm": "tsc && tsc-alias", + "lint": "eslint . --ext .ts", + "lint:fix": "eslint . --ext .ts --fix", + "clean": "rimraf ./dist", + "test:browser": "vitest run --browser.name=chrome --browser.headless --browser.provider=webdriverio", + "test:node": "vitest --run", + "test": "npm run test:node && npm run test:browser" + }, + "repository": "git://github.com/microsoft/kiota-typescript.git", + "keywords": [ + "kiota", + "openAPI", + "Microsoft", + "Graph" + ], + "author": "Microsoft", + "license": "MIT", + "bugs": { + "url": "https://github.com/microsoft/kiota-typescript/issues" + }, + "homepage": "https://github.com/microsoft/kiota#readme", + "devDependencies": { + }, + "dependencies": { + "@microsoft/kiota-abstractions": "*", + "@microsoft/kiota-http-fetchlibrary": "*", + "@microsoft/kiota-serialization-form": "*", + "@microsoft/kiota-serialization-json": "*", + "@microsoft/kiota-serialization-text": "*", + "@microsoft/kiota-serialization-multipart": "*" + }, + "publishConfig": { + "access": "public" + } +} diff --git a/packages/bundle/src/defaultRequestAdapter.ts b/packages/bundle/src/defaultRequestAdapter.ts new file mode 100644 index 000000000..4fc14bae6 --- /dev/null +++ b/packages/bundle/src/defaultRequestAdapter.ts @@ -0,0 +1,41 @@ +/** + * ------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. + * See License in the project root for license information. + * ------------------------------------------------------------------------------------------- + */ + +import { AuthenticationProvider, ParseNodeFactory, ParseNodeFactoryRegistry, registerDefaultDeserializer, registerDefaultSerializer, SerializationWriterFactory, SerializationWriterFactoryRegistry } from "@microsoft/kiota-abstractions"; +import { FormParseNodeFactory, FormSerializationWriterFactory } from "@microsoft/kiota-serialization-form"; +import { JsonParseNodeFactory, JsonSerializationWriterFactory } from "@microsoft/kiota-serialization-json"; +import { MultipartSerializationWriterFactory } from "@microsoft/kiota-serialization-multipart"; +import { TextParseNodeFactory, TextSerializationWriterFactory } from "@microsoft/kiota-serialization-text"; +import { FetchRequestAdapter, HttpClient, type ObservabilityOptions, ObservabilityOptionsImpl } from "@microsoft/kiota-http-fetchlibrary"; + +/** + * Default request adapter for graph clients. Bootstraps serialization and other aspects. + */ +export class DefaultRequestAdapter extends FetchRequestAdapter { + /** + * Instantiates a new request adapter. + * @param authenticationProvider the authentication provider to use. + * @param parseNodeFactory the parse node factory to deserialize responses. + * @param serializationWriterFactory the serialization writer factory to use to serialize request bodies. + * @param httpClient the http client to use to execute requests. + * @param observabilityOptions the observability options to use. + */ + public constructor(authenticationProvider: AuthenticationProvider, parseNodeFactory: ParseNodeFactory = ParseNodeFactoryRegistry.defaultInstance, serializationWriterFactory: SerializationWriterFactory = SerializationWriterFactoryRegistry.defaultInstance, httpClient: HttpClient = new HttpClient(), observabilityOptions: ObservabilityOptions = new ObservabilityOptionsImpl()) { + super(authenticationProvider, parseNodeFactory, serializationWriterFactory, httpClient, observabilityOptions); + DefaultRequestAdapter.setupDefaults(); + } + + private static setupDefaults() { + registerDefaultSerializer(JsonSerializationWriterFactory); + registerDefaultSerializer(TextSerializationWriterFactory); + registerDefaultSerializer(FormSerializationWriterFactory); + registerDefaultSerializer(MultipartSerializationWriterFactory); + registerDefaultDeserializer(JsonParseNodeFactory); + registerDefaultDeserializer(TextParseNodeFactory); + registerDefaultDeserializer(FormParseNodeFactory); + } +} diff --git a/packages/bundle/src/index.ts b/packages/bundle/src/index.ts new file mode 100644 index 000000000..bfd236bcd --- /dev/null +++ b/packages/bundle/src/index.ts @@ -0,0 +1,10 @@ +/** + * ------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. + * See License in the project root for license information. + * ------------------------------------------------------------------------------------------- + */ + +/* eslint-disable @typescript-eslint/triple-slash-reference*/ + +export * from "./defaultRequestAdapter"; diff --git a/packages/bundle/test/bundleTests.ts b/packages/bundle/test/bundleTests.ts new file mode 100644 index 000000000..c9fd9dcb2 --- /dev/null +++ b/packages/bundle/test/bundleTests.ts @@ -0,0 +1,45 @@ +/** + * ------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. + * See License in the project root for license information. + * ------------------------------------------------------------------------------------------- + */ +import { AnonymousAuthenticationProvider, AuthenticationProvider, ParseNodeFactoryRegistry, SerializationWriterFactoryRegistry } from "@microsoft/kiota-abstractions"; +import { assert, describe, it } from "vitest"; + +import { DefaultRequestAdapter } from "../src/defaultRequestAdapter"; + +describe("defaultRequestAdapter.ts", () => { + describe("Initialization", () => { + it("should throw error on null authProvider", async () => { + assert.throws(() => new DefaultRequestAdapter(null as unknown as AuthenticationProvider), Error, "authentication provider cannot be null"); + }); + + it("should setup serializers correctly", async () => { + const requestAdapter = new DefaultRequestAdapter(new AnonymousAuthenticationProvider()); + + assert.isNotNull(requestAdapter); + + const serializerMap = SerializationWriterFactoryRegistry.defaultInstance.contentTypeAssociatedFactories; + const deserializerMap = ParseNodeFactoryRegistry.defaultInstance.contentTypeAssociatedFactories; + + assert.isNotNull(serializerMap); + assert.isNotNull(deserializerMap); + + // verify that the default serializers are registered + assert.equal(serializerMap.size, 4); + assert.equal(deserializerMap.size, 3); + + // verify that the default serializers are registered by name + assert.isTrue(serializerMap.has("application/json")); + assert.isTrue(serializerMap.has("text/plain")); + assert.isTrue(serializerMap.has("application/x-www-form-urlencoded")); + assert.isTrue(serializerMap.has("multipart/form-data")); + + // verify that the default deserializers are registered by name + assert.isTrue(deserializerMap.has("application/json")); + assert.isTrue(deserializerMap.has("text/plain")); + assert.isTrue(deserializerMap.has("application/x-www-form-urlencoded")); + }); + }); +}); diff --git a/packages/bundle/tsconfig.json b/packages/bundle/tsconfig.json new file mode 100644 index 000000000..5e72b51a9 --- /dev/null +++ b/packages/bundle/tsconfig.json @@ -0,0 +1,10 @@ +{ + "$schema": "https://json.schemastore.org/tsconfig", + "extends": "../../tsconfig.base.json", + "compilerOptions": { + "outDir": "dist/es/" + }, + "include": [ + "./src/**/*.ts", + ] +} diff --git a/packages/bundle/vite.config.mts b/packages/bundle/vite.config.mts new file mode 100644 index 000000000..4a6f89f2f --- /dev/null +++ b/packages/bundle/vite.config.mts @@ -0,0 +1,11 @@ +import { defineConfig, configDefaults } from "vitest/config"; + +export default defineConfig({ + test: { + exclude: [...configDefaults.exclude, "**/test{Entity,Enum}.ts"], + include: [...configDefaults.include, "test/**/*.ts"], + coverage: { + reporter: ["html"], + }, + }, +}); \ No newline at end of file diff --git a/packages/http/fetch/package.json b/packages/http/fetch/package.json index 89f9c0eac..6d834acd9 100644 --- a/packages/http/fetch/package.json +++ b/packages/http/fetch/package.json @@ -1,6 +1,6 @@ { "name": "@microsoft/kiota-http-fetchlibrary", - "version": "1.0.0-preview.56", + "version": "1.0.0-preview.57", "description": "Kiota request adapter implementation with fetch", "keywords": [ "Kiota", diff --git a/packages/serialization/form/package.json b/packages/serialization/form/package.json index 5bfcdc851..35cb76ba6 100644 --- a/packages/serialization/form/package.json +++ b/packages/serialization/form/package.json @@ -1,6 +1,6 @@ { "name": "@microsoft/kiota-serialization-form", - "version": "1.0.0-preview.46", + "version": "1.0.0-preview.47", "description": "Implementation of Kiota Serialization interfaces for URI from encoded", "main": "dist/es/src/index.js", "browser": { diff --git a/packages/serialization/json/package.json b/packages/serialization/json/package.json index 151dd2fa6..e0a6d2afd 100644 --- a/packages/serialization/json/package.json +++ b/packages/serialization/json/package.json @@ -1,6 +1,6 @@ { "name": "@microsoft/kiota-serialization-json", - "version": "1.0.0-preview.57", + "version": "1.0.0-preview.58", "description": "Implementation of Kiota Serialization interfaces for JSON", "main": "dist/es/src/index.js", "type": "module", diff --git a/packages/serialization/multipart/package.json b/packages/serialization/multipart/package.json index fe0548a55..9587030e6 100644 --- a/packages/serialization/multipart/package.json +++ b/packages/serialization/multipart/package.json @@ -1,6 +1,6 @@ { "name": "@microsoft/kiota-serialization-multipart", - "version": "1.0.0-preview.35", + "version": "1.0.0-preview.36", "description": "Implementation of Kiota Serialization interfaces for multipart form data", "main": "dist/es/src/index.js", "module": "dist/es/src/index.js", diff --git a/packages/serialization/text/package.json b/packages/serialization/text/package.json index bb61b8e10..735297c41 100644 --- a/packages/serialization/text/package.json +++ b/packages/serialization/text/package.json @@ -1,6 +1,6 @@ { "name": "@microsoft/kiota-serialization-text", - "version": "1.0.0-preview.54", + "version": "1.0.0-preview.55", "description": "Implementation of Kiota Serialization interfaces for text", "main": "dist/es/src/index.js", "browser": {