Skip to content

Commit

Permalink
Merge pull request #1298 from microsoft/andrueastman/kiota-bundle
Browse files Browse the repository at this point in the history
Setup bundle for TS
  • Loading branch information
andrueastman authored Jul 31, 2024
2 parents fcc6011 + cddd14d commit c11f92d
Show file tree
Hide file tree
Showing 21 changed files with 214 additions and 8 deletions.
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"packages": [
"packages/test",
"packages/abstractions",
"packages/bundle",
"packages/http/*",
"packages/serialization/*",
"packages/authentication/*"
Expand Down
18 changes: 18 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"workspaces": [
"packages/test/",
"packages/abstractions",
"packages/bundle",
"packages/http/*",
"packages/serialization/*",
"packages/authentication/*"
Expand Down
2 changes: 1 addition & 1 deletion packages/abstractions/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/authentication/azure/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/authentication/spfx/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 2 additions & 0 deletions packages/bundle/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
*.tsbuildinfo
11 changes: 11 additions & 0 deletions packages/bundle/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.eslint**
.prettier**
*.log
*.tgz
**/test/**/*
docs/
node_modules/
rollup.config.js
src/
tsconfig.**.json
vite.config.mts
10 changes: 10 additions & 0 deletions packages/bundle/README.md
Original file line number Diff line number Diff line change
@@ -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`
45 changes: 45 additions & 0 deletions packages/bundle/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
41 changes: 41 additions & 0 deletions packages/bundle/src/defaultRequestAdapter.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
10 changes: 10 additions & 0 deletions packages/bundle/src/index.ts
Original file line number Diff line number Diff line change
@@ -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";
45 changes: 45 additions & 0 deletions packages/bundle/test/bundleTests.ts
Original file line number Diff line number Diff line change
@@ -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"));
});
});
});
10 changes: 10 additions & 0 deletions packages/bundle/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "dist/es/"
},
"include": [
"./src/**/*.ts",
]
}
11 changes: 11 additions & 0 deletions packages/bundle/vite.config.mts
Original file line number Diff line number Diff line change
@@ -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"],
},
},
});
2 changes: 1 addition & 1 deletion packages/http/fetch/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/serialization/form/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion packages/serialization/json/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/serialization/multipart/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/serialization/text/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit c11f92d

Please sign in to comment.