Skip to content

Commit

Permalink
Merge pull request #1475 from markovav-official/feature/custom-guid-type
Browse files Browse the repository at this point in the history
fix: getting rid of unnecessary guid-typescript dependency
  • Loading branch information
andrueastman authored Nov 7, 2024
2 parents 50868c8 + 7a7c9ed commit ced60d1
Show file tree
Hide file tree
Showing 19 changed files with 45 additions and 50 deletions.
12 changes: 0 additions & 12 deletions package-lock.json

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

1 change: 0 additions & 1 deletion packages/abstractions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"dependencies": {
"@opentelemetry/api": "^1.7.0",
"@std-uritemplate/std-uritemplate": "^1.0.1",
"guid-typescript": "^1.0.9",
"tinyduration": "^3.3.0",
"tslib": "^2.6.2",
"uuid": "^11.0.2"
Expand Down
4 changes: 2 additions & 2 deletions packages/abstractions/src/multipartBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
import { Guid } from "guid-typescript";
import type { RequestAdapter } from "./requestAdapter";
import type { ModelSerializerFunction, Parsable, ParseNode, SerializationWriter } from "./serialization";
import { createGuid } from "./utils/guidUtils";

/**
* Defines an interface for a multipart body for request or response.
Expand All @@ -19,7 +19,7 @@ export class MultipartBody implements Parsable {
* Instantiates a new MultipartBody.
*/
public constructor() {
this._boundary = Guid.create().toString().replace(/-/g, "");
this._boundary = createGuid().replace(/-/g, "");
}
/**
* Adds or replaces a part with the given name, content type and content.
Expand Down
5 changes: 2 additions & 3 deletions packages/abstractions/src/requestInformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import type { RequestConfiguration } from "./requestConfiguration";
import type { RequestOption } from "./requestOption";
import type { ModelSerializerFunction, Parsable, SerializationWriter } from "./serialization";
import { TimeOnly } from "./timeOnly";
import { Guid } from "guid-typescript";

/** This class represents an abstract HTTP request. */
export class RequestInformation implements RequestInformationSetContent {
Expand Down Expand Up @@ -241,7 +240,7 @@ export class RequestInformation implements RequestInformationSetContent {
};

private normalizeValue(value: unknown): unknown {
if (value instanceof Guid || value instanceof DateOnly || value instanceof TimeOnly) {
if (value instanceof DateOnly || value instanceof TimeOnly) {
return value.toString();
}
if (value instanceof Date) {
Expand All @@ -268,7 +267,7 @@ export class RequestInformation implements RequestInformationSetContent {
}
}
if (typeof v === "boolean" || typeof v === "number" || typeof v === "string" || Array.isArray(v)) this.queryParameters[key] = v;
else if (v instanceof Guid || v instanceof DateOnly || v instanceof TimeOnly) this.queryParameters[key] = v.toString();
else if (v instanceof DateOnly || v instanceof TimeOnly) this.queryParameters[key] = v.toString();
else if (v instanceof Date) this.queryParameters[key] = v.toISOString();
else if (v === undefined) this.queryParameters[key] = undefined;
});
Expand Down
2 changes: 1 addition & 1 deletion packages/abstractions/src/serialization/parseNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
import type { Guid } from "guid-typescript";
import type { Guid } from "../utils/guidUtils";

import type { DateOnly } from "../dateOnly";
import type { Duration } from "../duration";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
import type { Guid } from "guid-typescript";
import type { Guid } from "../utils/guidUtils";

import type { DateOnly } from "../dateOnly";
import type { Duration } from "../duration";
Expand Down
41 changes: 31 additions & 10 deletions packages/abstractions/src/utils/guidUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,38 @@
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
import { Guid } from "guid-typescript";
export type Guid = string;

const guidValidator = new RegExp("^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$", "i");

/**
* Checks if the string is a valid GUID.
* @param source the string to check.
* @returns unchanged string if it is a valid GUID; otherwise, undefined.
*/
export const parseGuidString = (source?: string) => {
if (source && guidValidator.test(source)) {
return source;
}
return undefined;
};

/**
* Generates a GUID.
* @returns a GUID.
*/
export const createGuid = () => [gen(2), gen(1), gen(1), gen(1), gen(3)].join("-");

/**
* Parses a string into a Guid object.
* @param source The source string.
* @returns The Guid object.
* Generates a part of a GUID.
* @param count the number of 2 byte chunks to generate.
* @returns a part of a GUID.
*/
export function parseGuidString(source?: string): Guid | undefined {
if (source && Guid.isGuid(source)) {
return Guid.parse(source);
} else {
return undefined;
export const gen = (count: number) => {
let out = "";
for (let i = 0; i < count; i++) {
// eslint-disable-next-line no-bitwise
out += (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
}
return out;
};
5 changes: 2 additions & 3 deletions packages/abstractions/test/common/requestInformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

import { assert, describe, it } from "vitest";

import { DateOnly, HttpMethod, type Parsable, parseGuidString, type RequestAdapter, RequestInformation, type SerializationWriter, type SerializationWriterFactory, TimeOnly } from "../../src";
import { DateOnly, HttpMethod, type Guid, type Parsable, parseGuidString, type RequestAdapter, RequestInformation, type SerializationWriter, type SerializationWriterFactory, TimeOnly } from "../../src";
import { MultipartBody } from "../../src/multipartBody";
import { TestEnum } from "./store/testEnum";
import { Guid } from "guid-typescript";

interface GetQueryParameters {
select?: string[];
Expand Down Expand Up @@ -225,7 +224,7 @@ describe("RequestInformation", () => {
const expected: string = `http://localhost/users/33933a8d-32bb-c6a8-784a-f60b5a1dd66a/2021-12-12?objectId=83afbf49-5583-152c-d7fb-176105d518bc&startDate=2021-12-12&startTime=23%3A12%3A00.0000000&timeStamp=2024-06-11T00%3A00%3A00.000Z`;
const requestInformation = new RequestInformation(HttpMethod.GET);
requestInformation.pathParameters["baseurl"] = baseUrl;
requestInformation.pathParameters["userId"] = Guid.parse("33933a8d-32bb-c6a8-784a-f60b5a1dd66a");
requestInformation.pathParameters["userId"] = parseGuidString("33933a8d-32bb-c6a8-784a-f60b5a1dd66a");
requestInformation.pathParameters["date"] = DateOnly.parse("2021-12-12");
requestInformation.urlTemplate = "http://localhost/users/{userId}/{date}{?objectId,startDate,startTime,endDate,endTime,timeStamp}";
requestInformation.setQueryStringParametersFromRawObject<GetQueryParameters>({ objectId: parseGuidString("83afbf49-5583-152c-d7fb-176105d518bc"), startDate: new DateOnly({ year: 2021, month: 12, day: 12 }), startTime: new TimeOnly({ hours: 23, minutes: 12 }), timeStamp: new Date("2024-06-11T00:00:00.000Z") }, getQueryParameterMapper);
Expand Down
1 change: 0 additions & 1 deletion packages/http/fetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"dependencies": {
"@microsoft/kiota-abstractions": "^1.0.0-preview.73",
"@opentelemetry/api": "^1.7.0",
"guid-typescript": "^1.0.9",
"tslib": "^2.6.2"
},
"publishConfig": {
Expand Down
3 changes: 1 addition & 2 deletions packages/http/fetch/test/common/mockParseNodeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
*/

/* eslint-disable @typescript-eslint/no-unused-vars */
import type { DateOnly, Duration, Parsable, ParsableFactory, ParseNode, ParseNodeFactory, TimeOnly } from "@microsoft/kiota-abstractions";
import type { Guid } from "guid-typescript";
import type { DateOnly, Duration, Guid, Parsable, ParsableFactory, ParseNode, ParseNodeFactory, TimeOnly } from "@microsoft/kiota-abstractions";

export class MockParseNodeFactory implements ParseNodeFactory {
parseNodeValue: ParseNode;
Expand Down
1 change: 0 additions & 1 deletion packages/serialization/form/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"homepage": "https://github.com/microsoft/kiota-typescript#readme",
"dependencies": {
"@microsoft/kiota-abstractions": "^1.0.0-preview.73",
"guid-typescript": "^1.0.9",
"tslib": "^2.6.2"
},
"publishConfig": {
Expand Down
3 changes: 1 addition & 2 deletions packages/serialization/form/src/formSerializationWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
*/

/* eslint-disable @typescript-eslint/no-unused-expressions */
import { DateOnly, Duration, type ModelSerializerFunction, type Parsable, type SerializationWriter, TimeOnly } from "@microsoft/kiota-abstractions";
import type { Guid } from "guid-typescript";
import { DateOnly, Duration, type Guid, type ModelSerializerFunction, type Parsable, type SerializationWriter, TimeOnly } from "@microsoft/kiota-abstractions";

export class FormSerializationWriter implements SerializationWriter {
public writeByteArrayValue(
Expand Down
1 change: 0 additions & 1 deletion packages/serialization/json/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"homepage": "https://github.com/microsoft/kiota-typescript#readme",
"dependencies": {
"@microsoft/kiota-abstractions": "^1.0.0-preview.73",
"guid-typescript": "^1.0.9",
"tslib": "^2.6.2"
},
"publishConfig": {
Expand Down
3 changes: 1 addition & 2 deletions packages/serialization/json/src/jsonSerializationWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
*/

/* eslint-disable @typescript-eslint/no-unused-expressions */
import { DateOnly, Duration, isUntypedNode, type ModelSerializerFunction, type Parsable, type SerializationWriter, TimeOnly, type UntypedNode, isUntypedBoolean, isUntypedString, isUntypedNull, isUntypedNumber, isUntypedObject, isUntypedArray, inNodeEnv } from "@microsoft/kiota-abstractions";
import type { Guid } from "guid-typescript";
import { DateOnly, Duration, type Guid, isUntypedNode, type ModelSerializerFunction, type Parsable, type SerializationWriter, TimeOnly, type UntypedNode, isUntypedBoolean, isUntypedString, isUntypedNull, isUntypedNumber, isUntypedObject, isUntypedArray, inNodeEnv } from "@microsoft/kiota-abstractions";

export class JsonSerializationWriter implements SerializationWriter {
public writeByteArrayValue(key?: string, value?: ArrayBuffer): void {
Expand Down
3 changes: 1 addition & 2 deletions packages/serialization/json/test/common/testEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
* -------------------------------------------------------------------------------------------
*/

import type { BackedModel, BackingStore, Parsable, ParseNode, SerializationWriter } from "@microsoft/kiota-abstractions";
import { Guid } from "guid-typescript";
import type { BackedModel, BackingStore, Guid, Parsable, ParseNode, SerializationWriter } from "@microsoft/kiota-abstractions";

const fakeBackingStore: BackingStore = {} as BackingStore;

Expand Down
1 change: 0 additions & 1 deletion packages/serialization/multipart/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"homepage": "https://github.com/microsoft/kiota-typescript#readme",
"dependencies": {
"@microsoft/kiota-abstractions": "^1.0.0-preview.73",
"guid-typescript": "^1.0.9",
"tslib": "^2.6.2"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
*/

/* eslint-disable @typescript-eslint/no-unused-expressions */
import { type DateOnly, type Duration, MultipartBody, type Parsable, type SerializationWriter, type ModelSerializerFunction, type TimeOnly } from "@microsoft/kiota-abstractions";
import type { Guid } from "guid-typescript";
import { type DateOnly, type Duration, type Guid, MultipartBody, type Parsable, type SerializationWriter, type ModelSerializerFunction, type TimeOnly } from "@microsoft/kiota-abstractions";

/** Serialization writer for multipart/form-data */
export class MultipartSerializationWriter implements SerializationWriter {
Expand Down
1 change: 0 additions & 1 deletion packages/serialization/text/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"homepage": "https://github.com/microsoft/kiota-typescript#readme",
"dependencies": {
"@microsoft/kiota-abstractions": "^1.0.0-preview.73",
"guid-typescript": "^1.0.9",
"tslib": "^2.6.2"
},
"publishConfig": {
Expand Down
3 changes: 1 addition & 2 deletions packages/serialization/text/src/textSerializationWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
*/

/* eslint-disable @typescript-eslint/no-unused-vars */
import { inNodeEnv, type DateOnly, type Duration, type ModelSerializerFunction, type Parsable, type SerializationWriter, type TimeOnly } from "@microsoft/kiota-abstractions";
import type { Guid } from "guid-typescript";
import { inNodeEnv, type DateOnly, type Duration, type Guid, type ModelSerializerFunction, type Parsable, type SerializationWriter, type TimeOnly } from "@microsoft/kiota-abstractions";

export class TextSerializationWriter implements SerializationWriter {
public writeByteArrayValue(key?: string, value?: ArrayBuffer | null): void {
Expand Down

0 comments on commit ced60d1

Please sign in to comment.