grpc-web-from-object
Advanced tools
Comparing version 2.4.1 to 2.5.0
# Changelog | ||
## [2.5.0](https://github.com/infodusha/grpc-web-from-object/compare/v2.4.1...v2.5.0) (2023-03-15) | ||
### Features | ||
* refactor types a bit & make prefix enum ([2838600](https://github.com/infodusha/grpc-web-from-object/commit/2838600ad0f2b135ad19f8cf68cb0d3327f5bf5e)) | ||
## [2.4.1](https://github.com/infodusha/grpc-web-from-object/compare/v2.4.0...v2.4.1) (2023-03-14) | ||
@@ -4,0 +11,0 @@ |
@@ -8,12 +8,10 @@ import { Message, Map as ProtobufMap } from "google-protobuf"; | ||
type IsMessageOrMessageArray<T> = T extends Array<infer R> ? IsMessageOrMessageArray<R> : T extends Message | undefined ? Exclude<T, undefined> : never; | ||
type GetMessageKeys<T extends Message> = { | ||
[K in keyof T]: K extends `get${string}` ? IsMessageOrMessageArray<IsProtobufMap<MessageFnReturnValue<T, K>>> extends never ? never : K : never; | ||
}[keyof T]; | ||
type MessageToObjectKey<T extends string> = T extends `get${infer R}` ? Uncapitalize<R> : never; | ||
type AsObjectToMessageKey<R> = R extends string ? `get${Capitalize<R>}` : never; | ||
type MessageValue<T extends Message, K extends keyof T> = IsMessageOrMessageArray<IsProtobufMap<MessageFnReturnValue<T, K>>>; | ||
type MessageKey<T extends Message, K extends keyof T = keyof T> = K extends `get${string}` ? MessageValue<T, K> extends never ? never : K : never; | ||
type Prop<K> = K extends `get${infer R}` ? Uncapitalize<R> : never; | ||
type MessageFactories<T extends Message> = { | ||
[K in MessageToObjectKey<GetMessageKeys<T>>]: FromObject<IsMessageOrMessageArray<IsProtobufMap<MessageFnReturnValue<T, AsObjectToMessageKey<K> extends keyof T ? AsObjectToMessageKey<K> : never>>>>; | ||
[K in MessageKey<T> as Prop<K>]: FromObject<MessageValue<T, K>>; | ||
}; | ||
type EmptyFactory<T extends Message> = GetMessageKeys<T> extends never ? T : never; | ||
type NonEmptyFactory<T extends Message> = GetMessageKeys<T> extends never ? never : T; | ||
type EmptyFactory<T extends Message> = MessageKey<T> extends never ? T : never; | ||
type NonEmptyFactory<T extends Message> = MessageKey<T> extends never ? never : T; | ||
export declare function createFromObject<T extends Message>(MessageType: MessageConstructor<EmptyFactory<T>>): FromObject<T>; | ||
@@ -20,0 +18,0 @@ export declare function createFromObject<T extends Message>(MessageType: MessageConstructor<NonEmptyFactory<T>>, factories: MessageFactories<T>): FromObject<T>; |
@@ -6,5 +6,2 @@ "use strict"; | ||
const recursiveFactories = new WeakMap(); | ||
const SETTER_PREFIX = 'set'; | ||
const GETTER_PREFIX = 'get'; | ||
const CLEAR_PREFIX = 'clear'; | ||
function createFromObject(MessageType, factories) { | ||
@@ -22,3 +19,3 @@ const allFactories = factories ?? {}; | ||
if (Array.isArray(value) && isProtobufMap(instance, prop)) { | ||
const mapMethod = getMethod(prop, GETTER_PREFIX); | ||
const mapMethod = getMethod(prop, "get" /* PREFIX.GET */); | ||
const map = callMethod(instance, mapMethod); | ||
@@ -37,3 +34,3 @@ for (const [k, v] of value) { | ||
validateType(instance, prop, value); | ||
const setter = getMethod(prop); | ||
const setter = getMethod(prop, "set" /* PREFIX.SET */); | ||
callMethod(instance, setter, result); | ||
@@ -62,7 +59,7 @@ } | ||
} | ||
function getProp(key, prefix = SETTER_PREFIX) { | ||
function getProp(key, prefix) { | ||
const prop = key.slice(prefix.length); | ||
return prop.slice(0, 1).toLowerCase() + prop.slice(1); | ||
} | ||
function getMethod(prop, prefix = SETTER_PREFIX) { | ||
function getMethod(prop, prefix) { | ||
return `${prefix}${prop[0].toUpperCase()}${prop.slice(1)}`; | ||
@@ -77,11 +74,11 @@ } | ||
const keys = Object.keys(Object.getPrototypeOf(instance)); | ||
const setters = getInstancePropsFromKeys(keys, SETTER_PREFIX); | ||
const maps = getInstancePropsFromKeys(keys, CLEAR_PREFIX).filter(prop => isProtobufMap(instance, prop)); | ||
const setters = getInstancePropsFromKeys(keys, "set" /* PREFIX.SET */); | ||
const maps = getInstancePropsFromKeys(keys, "clear" /* PREFIX.CLEAR */).filter(prop => isProtobufMap(instance, prop)); | ||
return [...setters, ...maps]; | ||
} | ||
function isProtobufMap(instance, prop) { | ||
return callMethod(instance, getMethod(prop, GETTER_PREFIX)) instanceof google_protobuf_1.Map; | ||
return callMethod(instance, getMethod(prop, "get" /* PREFIX.GET */)) instanceof google_protobuf_1.Map; | ||
} | ||
function isOptional(instance, prop) { | ||
const clearMethod = getMethod(prop, CLEAR_PREFIX); | ||
const clearMethod = getMethod(prop, "clear" /* PREFIX.CLEAR */); | ||
return clearMethod in instance; | ||
@@ -123,3 +120,3 @@ } | ||
function validateType(instance, prop, value) { | ||
const getter = getMethod(prop, GETTER_PREFIX); | ||
const getter = getMethod(prop, "get" /* PREFIX.GET */); | ||
const instanceValue = callMethod(instance, getter); | ||
@@ -126,0 +123,0 @@ const expectedType = instanceValue !== undefined ? typeof instanceValue : 'object'; |
{ | ||
"name": "grpc-web-from-object", | ||
"version": "2.4.1", | ||
"version": "2.5.0", | ||
"description": "fromObject method for grpc-web", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26375
164