Skip to content

Commit 2d6afda

Browse files
authored
Merge pull request #60 from ferflores507/dev
Dev
2 parents 741e8c2 + 9fd6966 commit 2d6afda

File tree

7 files changed

+131
-49
lines changed

7 files changed

+131
-49
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ferflores507/object-builder",
3-
"version": "9.8.1",
3+
"version": "9.9.0",
44
"main": "index.js",
55
"scripts": {
66
"test": "vitest"

src/builders/Operators.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Builder, ChildrenSchema, OperatorTask, Propiedades, Schema, SchemaDefinition, WithTaskOptions } from "../models"
1+
import { Builder, ChildrenSchema, DebounceOptions, DebounceSchema, OperatorTask, Propiedades, RequestInitWithUrl, RequestPlainOptions, Schema, SchemaDefinition, WithTaskOptions } from "../models"
22
import { reduceRequest, type RequestInfo } from "../helpers/requestHelper"
33

44
import {
@@ -10,8 +10,6 @@ import {
1010
formatSortOptions,
1111
Path,
1212
removeAccents,
13-
RequestInitWithUrl,
14-
type RequestPlainOptions,
1513
sortCompare,
1614
type SortOptions,
1715
spread,
@@ -56,8 +54,26 @@ export class Operators implements WithTaskOptions<Operators> {
5654
date = (value: any, options: Intl.DateTimeFormatOptions & { locale: string }) => {
5755
return new Date(value).toLocaleString(options.locale, options)
5856
}
59-
debounce = (fn: (...args: []) => any, ms: number | true) => {
60-
return createDebounce(fn, ms === true ? 500 : ms)
57+
debounce = (fn: Function, ms: number | true | Function) => {
58+
if(typeof ms == "function") {
59+
fn = ms
60+
ms = true
61+
}
62+
63+
return createDebounce(fn, typeof ms == "number" ? ms : 500)
64+
}
65+
debounceWith = {
66+
transform: ({ function: fn, ms, target }: DebounceSchema) => {
67+
return {
68+
propiedades: {
69+
function: target ? target : { function: fn },
70+
ms
71+
}
72+
}
73+
},
74+
task: (initial: any, { function: fn, ms }: DebounceOptions) => {
75+
return this.debounce(fn, ms)
76+
}
6177
}
6278
entries = entries
6379
spreadStart = (target: any[], value: any) => {
@@ -131,8 +147,9 @@ export class Operators implements WithTaskOptions<Operators> {
131147
},
132148
task: (initial: Record<string, any>, current: Record<string, any>) => current
133149
}
134-
request = (initial: any, options: RequestPlainOptions) => {
135-
return buildRequest(options)
150+
request = {
151+
transform: (propiedades: Propiedades) => ({ propiedades }),
152+
task: (initial: any, options: RequestPlainOptions) => buildRequest(options)
136153
}
137154
reduceFetch = (requestInit: RequestInitWithUrl, id: any, builder: Builder) => {
138155
const requestInfo: RequestInfo = {

src/helpers/varios.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
type Modify<T, R> = Omit<T, keyof R> & R;
1+
import { RequestInitWithUrl, RequestPlainOptions } from "../models"
22

33
export const fetchHelper = async (request: RequestInitWithUrl, init?: RequestInit) => {
44
// Verify if its ok to add signal in 2nd param object or spread to request
@@ -14,22 +14,12 @@ export const fetchHelper = async (request: RequestInitWithUrl, init?: RequestIni
1414
return response.json()
1515
}
1616

17-
export type RequestPlainOptions = Modify<Partial<Request>, {
18-
url: string
19-
contentType?: typeof applicationJson
20-
query?: Record<string, any>
21-
body?: Record<string, any>
22-
formData?: Record<string, any>
23-
}>
24-
2517
export const urlWithCleanQueryString = (url: string, query: Record<string, any>) => {
2618
const queryString = new URLSearchParams(removeNullOrUndefined(query)).toString()
2719

2820
return url + (queryString && "?") + queryString
2921
}
3022

31-
export type RequestInitWithUrl = Modify<RequestInit, { url: string }>
32-
3323
export class RequestWithUrl extends Request {
3424
constructor(input: RequestInitWithUrl) {
3525
super(input.url, input)
@@ -99,7 +89,7 @@ const argsPairFn = () => {
9989

10090
export const argsPair = argsPairFn()
10191

102-
export const createDebounce = (callback: (...args: any[]) => any, ms: number) => {
92+
export const createDebounce = (callback: Function, ms: number) => {
10393
let timeoutId: NodeJS.Timeout
10494

10595
return (...args: any[]) => {

src/models.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
import { Path } from "./helpers/varios"
22

3+
type Modify<T, R> = Omit<T, keyof R> & R;
4+
5+
export type RequestInitWithUrl = Modify<RequestInit, { url: string }>
6+
7+
export type RequestPlainOptions = { url: string } & Partial<Modify<Request, {
8+
contentType: string
9+
query: Record<string, unknown>
10+
body: Record<string, any>
11+
formData: Record<string, any>
12+
}>>
13+
14+
export type RequestPlainOptionsSchema = { url: Schema }
15+
& {
16+
[P in keyof RequestPlainOptions]: Schema
17+
}
18+
& Partial<{
19+
body: SchemaDefinition
20+
formData: SchemaDefinition
21+
}>
22+
23+
export type RequestSchema = {
24+
[P in keyof RequestPlainOptionsSchema]: RequestPlainOptions[P] | RequestPlainOptionsSchema[P]
25+
} & {
26+
$bind?: Schema
27+
}
28+
329
export type ChildrenSchema = Partial<{
430
setup: SchemaDefinition
531
schema: SchemaDefinition
@@ -85,6 +111,20 @@ export type SchemaDefinition = Schema | { [n: number]: Schema | SchemaPrimitive
85111

86112
export type SchemaPrimitive = string | number | bigint | boolean | null
87113
export type Propiedades = Record<string, SchemaDefinition | SchemaPrimitive>
114+
export type DebounceOptions = {
115+
function: Function
116+
ms: number
117+
}
118+
119+
export type DebounceSchema = {
120+
ms: SchemaDefinition | number
121+
} & ({
122+
function: SchemaDefinition
123+
target?: undefined
124+
} | {
125+
function?: undefined
126+
target: SchemaDefinition
127+
})
88128

89129
export type Schema = Partial<{
90130
allEqual: boolean
@@ -104,6 +144,7 @@ export type Schema = Partial<{
104144
const: any
105145
date: Schema | SchemaPrimitive
106146
debounce: Schema | SchemaPrimitive
147+
debounceWith: DebounceSchema
107148
decrement: string
108149
default: SchemaDefinition | SchemaPrimitive
109150
definitions: (Schema | SchemaPrimitive)[]
@@ -150,7 +191,7 @@ export type Schema = Partial<{
150191
reduce: SchemaDefinition
151192
reduceOrDefault: SchemaDefinition
152193
removeAccents: true
153-
request: Schema
194+
request: RequestSchema
154195
reduceFetch: Schema | SchemaPrimitive
155196
required: string[]
156197
schemaFrom: SchemaDefinition

tests/schema/mixed.test.ts

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -585,12 +585,10 @@ test.skip("expect api response", async () => {
585585
reduce: [
586586
{
587587
request: {
588-
propiedades: {
589-
url: "https://api.the-odds-api.com/v4/sports",
590-
query: {
591-
propiedades: {
592-
apiKey: process.env.ODDS_API_KEY!
593-
}
588+
url: "https://api.the-odds-api.com/v4/sports",
589+
query: {
590+
propiedades: {
591+
apiKey: process.env.ODDS_API_KEY!
594592
}
595593
}
596594
},
@@ -1704,24 +1702,63 @@ describe("debounce", function () {
17041702
beforeEach(() => vi.useFakeTimers());
17051703
afterEach(() => vi.useRealTimers());
17061704

1707-
it("expects function call to set arg as 'store.nombre' value", () => {
1708-
const builder = new ObjectBuilder()
1709-
.with({
1710-
schema: {
1705+
describe("function call", () => {
1706+
const schemas: Schema[] = [
1707+
{
1708+
debounce: {
17111709
function: {
17121710
set: "nombre"
1713-
},
1714-
debounce: true
1711+
}
17151712
}
1716-
})
1717-
1718-
const debounceSet = builder.build()
1719-
1720-
debounceSet("Melany")
1721-
1722-
vi.runAllTimers()
1713+
},
1714+
{
1715+
function: {
1716+
set: "nombre"
1717+
},
1718+
debounce: true
1719+
},
1720+
{
1721+
debounceWith: {
1722+
ms: 1000,
1723+
function: {
1724+
set: "nombre"
1725+
}
1726+
}
1727+
},
1728+
{
1729+
reduce: [
1730+
{
1731+
set: [
1732+
"setNombre",
1733+
{
1734+
function: {
1735+
set: "nombre"
1736+
}
1737+
}
1738+
]
1739+
},
1740+
{
1741+
debounceWith: {
1742+
ms: 1000,
1743+
target: {
1744+
path: "setNombre"
1745+
}
1746+
}
1747+
}
1748+
]
1749+
}
1750+
]
17231751

1724-
expect(builder.options.store.nombre).toBe("Melany")
1752+
it.each(schemas)("expects function call to set arg as 'store.nombre' value", schema => {
1753+
const builder = new ObjectBuilder().with({ schema })
1754+
const debounceSet = builder.build()
1755+
1756+
debounceSet("Melany")
1757+
1758+
vi.runAllTimers()
1759+
1760+
expect(builder.options.store.nombre).toBe("Melany")
1761+
})
17251762
})
17261763

17271764
const cases = [true, 300, 1000] as const

tests/schemaServer.test.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ describe("reduce fetch check requests length after each request", async () => {
6969
store,
7070
schema: {
7171
request: {
72-
propiedades: { url }
72+
url
7373
},
7474
reduceFetch: id
7575
}
@@ -90,9 +90,7 @@ test("reduce fetch", async () => {
9090
const result = await new ObjectBuilder()
9191
.withSchema({
9292
request: {
93-
propiedades: {
94-
url: "http://localhost:8000/numeros"
95-
}
93+
url: "http://localhost:8000/numeros"
9694
},
9795
reduceFetch: 1
9896
})
@@ -107,9 +105,7 @@ test("schema request", async () => {
107105
const request = new ObjectBuilder()
108106
.withSchema({
109107
request: {
110-
propiedades: {
111-
url: "http://localhost:8000/numeros"
112-
}
108+
url: "http://localhost:8000/numeros"
113109
}
114110
})
115111
.build()

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"compilerOptions": {
33
"strict": true,
4-
"target": "ES2024"
4+
"target": "ES2024",
5+
"moduleResolution": "node"
56
}
67
}

0 commit comments

Comments
 (0)