@praxisjs/decorators 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
# @praxisjs/decorators
|
|
2
2
|
|
|
3
|
+
## 1.0.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- cfb0de2: `@Resource` now accepts a self-receiving fetcher as an alternative to the zero-argument arrow function.
|
|
8
|
+
|
|
9
|
+
When the fetcher accepts one argument, the component instance is passed automatically at bind time — signal reads on `self` become reactive dependencies and TypeScript knows the type:
|
|
10
|
+
|
|
11
|
+
```tsx
|
|
12
|
+
// before — arrow function, 'this' was module scope (undefined)
|
|
13
|
+
@Resource(() => fetch(`/api/posts?page=${this.page}`).then(r => r.json()))
|
|
14
|
+
|
|
15
|
+
// after — receive the instance as 'self'
|
|
16
|
+
@Resource((self: PostList) => fetch(`/api/posts?page=${self.page}`).then(r => r.json()))
|
|
17
|
+
posts!: ResourceInstance<Post[]>
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
The zero-argument form is unchanged and continues to work for fetchers with no component dependency.
|
|
21
|
+
|
|
22
|
+
## 1.0.1
|
|
23
|
+
|
|
24
|
+
### Patch Changes
|
|
25
|
+
|
|
26
|
+
- Updated dependencies [74d414a]
|
|
27
|
+
- @praxisjs/core@1.6.0
|
|
28
|
+
|
|
3
29
|
## 1.0.0
|
|
4
30
|
|
|
5
31
|
### Major Changes
|
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
import { type Resource as ResourceInstance, type ResourceOptions } from "@praxisjs/core/internal";
|
|
2
2
|
export type { ResourceInstance, ResourceOptions };
|
|
3
|
+
type FieldDecoratorReturn = (_value: undefined, context: ClassFieldDecoratorContext) => void;
|
|
3
4
|
/**
|
|
4
|
-
* Binds an async resource to the field.
|
|
5
|
-
* with reactive data, pending, error, and status signals, plus refetch/cancel/mutate methods.
|
|
5
|
+
* Binds an async resource to the field.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
7
|
+
* No component dependency — plain arrow:
|
|
8
8
|
* @Resource(() => api.getUsers())
|
|
9
9
|
* users!: ResourceInstance<User[]>;
|
|
10
|
-
*
|
|
11
|
-
*
|
|
10
|
+
*
|
|
11
|
+
* Reads reactive component fields — receive the instance as `self`:
|
|
12
|
+
* @Resource((self: MyComponent) => api.getPage(self.page))
|
|
13
|
+
* items!: ResourceInstance<Item[]>;
|
|
12
14
|
*/
|
|
13
|
-
export declare function Resource<T>(fetcher: () => Promise<T>, options?: ResourceOptions<T>):
|
|
15
|
+
export declare function Resource<T>(fetcher: () => Promise<T>, options?: ResourceOptions<T>): FieldDecoratorReturn;
|
|
16
|
+
export declare function Resource<T, C extends object = any>(fetcher: (self: C) => Promise<T>, options?: ResourceOptions<T>): FieldDecoratorReturn;
|
|
14
17
|
//# sourceMappingURL=resource.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resource.d.ts","sourceRoot":"","sources":["../../src/properties/resource.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,QAAQ,IAAI,gBAAgB,EACjC,KAAK,eAAe,EACrB,MAAM,yBAAyB,CAAC;AAIjC,YAAY,EAAE,gBAAgB,EAAE,eAAe,EAAE,CAAC;AAElD
|
|
1
|
+
{"version":3,"file":"resource.d.ts","sourceRoot":"","sources":["../../src/properties/resource.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,QAAQ,IAAI,gBAAgB,EACjC,KAAK,eAAe,EACrB,MAAM,yBAAyB,CAAC;AAIjC,YAAY,EAAE,gBAAgB,EAAE,eAAe,EAAE,CAAC;AAElD,KAAK,oBAAoB,GAAG,CAAC,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,0BAA0B,KAAK,IAAI,CAAC;AAE7F;;;;;;;;;;GAUG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE,eAAe,CAAC,CAAC,CAAC,GAAG,oBAAoB,CAAC;AAE3G,wBAAgB,QAAQ,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,GAAG,GAAG,EAAE,OAAO,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE,eAAe,CAAC,CAAC,CAAC,GAAG,oBAAoB,CAAC"}
|
|
@@ -1,19 +1,15 @@
|
|
|
1
1
|
import { resource, } from "@praxisjs/core/internal";
|
|
2
2
|
import { createFieldDecorator } from "../create-field-decorator";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
*
|
|
7
|
-
* class MyComponent extends StatefulComponent {
|
|
8
|
-
* @Resource(() => api.getUsers())
|
|
9
|
-
* users!: ResourceInstance<User[]>;
|
|
10
|
-
* // access: this.users.data(), this.users.pending(), this.users.refetch()
|
|
11
|
-
* }
|
|
12
|
-
*/
|
|
13
|
-
export function Resource(fetcher, options = {}) {
|
|
3
|
+
export function Resource(
|
|
4
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
5
|
+
fetcher, options = {}) {
|
|
14
6
|
return createFieldDecorator({
|
|
15
|
-
bind(
|
|
16
|
-
const
|
|
7
|
+
bind(instance, _name, _initialValue) {
|
|
8
|
+
const bound = fetcher.length > 0
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
10
|
+
? () => fetcher(instance)
|
|
11
|
+
: fetcher;
|
|
12
|
+
const r = resource(bound, options);
|
|
17
13
|
return {
|
|
18
14
|
descriptor: {
|
|
19
15
|
get() { return r; },
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resource.js","sourceRoot":"","sources":["../../src/properties/resource.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,GAGT,MAAM,yBAAyB,CAAC;AAEjC,OAAO,EAAE,oBAAoB,EAAqB,MAAM,2BAA2B,CAAC;
|
|
1
|
+
{"version":3,"file":"resource.js","sourceRoot":"","sources":["../../src/properties/resource.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,GAGT,MAAM,yBAAyB,CAAC;AAEjC,OAAO,EAAE,oBAAoB,EAAqB,MAAM,2BAA2B,CAAC;AAoBpF,MAAM,UAAU,QAAQ;AACtB,8DAA8D;AAC9D,OAAyD,EACzD,UAA8B,EAAE;IAEhC,OAAO,oBAAoB,CAAC;QAC1B,IAAI,CAAC,QAAQ,EAAE,KAAK,EAAE,aAAa;YACjC,MAAM,KAAK,GACT,OAAO,CAAC,MAAM,GAAG,CAAC;gBAChB,8DAA8D;gBAC9D,CAAC,CAAC,GAAG,EAAE,CAAE,OAAqC,CAAC,QAAQ,CAAC;gBACxD,CAAC,CAAE,OAA4B,CAAC;YACpC,MAAM,CAAC,GAAG,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;YACnC,OAAO;gBACL,UAAU,EAAE;oBACV,GAAG,KAA0B,OAAO,CAAC,CAAC,CAAC,CAAC;oBACxC,gEAAgE;oBAChE,GAAG,KAAU,CAAC;iBACf;aACF,CAAC;QACJ,CAAC;KACF,CAAoC,CAAC;AACxC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@praxisjs/decorators",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"typescript": "^5.9.3"
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@praxisjs/core": "1.
|
|
18
|
+
"@praxisjs/core": "1.6.0",
|
|
19
19
|
"@praxisjs/shared": "0.2.0"
|
|
20
20
|
},
|
|
21
21
|
"scripts": {
|
|
@@ -8,20 +8,35 @@ import { createFieldDecorator, type FieldBinding } from "../create-field-decorat
|
|
|
8
8
|
|
|
9
9
|
export type { ResourceInstance, ResourceOptions };
|
|
10
10
|
|
|
11
|
+
type FieldDecoratorReturn = (_value: undefined, context: ClassFieldDecoratorContext) => void;
|
|
12
|
+
|
|
11
13
|
/**
|
|
12
|
-
* Binds an async resource to the field.
|
|
13
|
-
* with reactive data, pending, error, and status signals, plus refetch/cancel/mutate methods.
|
|
14
|
+
* Binds an async resource to the field.
|
|
14
15
|
*
|
|
15
|
-
*
|
|
16
|
+
* No component dependency — plain arrow:
|
|
16
17
|
* @Resource(() => api.getUsers())
|
|
17
18
|
* users!: ResourceInstance<User[]>;
|
|
18
|
-
*
|
|
19
|
-
*
|
|
19
|
+
*
|
|
20
|
+
* Reads reactive component fields — receive the instance as `self`:
|
|
21
|
+
* @Resource((self: MyComponent) => api.getPage(self.page))
|
|
22
|
+
* items!: ResourceInstance<Item[]>;
|
|
20
23
|
*/
|
|
21
|
-
export function Resource<T>(fetcher: () => Promise<T>, options
|
|
24
|
+
export function Resource<T>(fetcher: () => Promise<T>, options?: ResourceOptions<T>): FieldDecoratorReturn;
|
|
25
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unnecessary-type-parameters
|
|
26
|
+
export function Resource<T, C extends object = any>(fetcher: (self: C) => Promise<T>, options?: ResourceOptions<T>): FieldDecoratorReturn;
|
|
27
|
+
export function Resource<T>(
|
|
28
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
29
|
+
fetcher: (() => Promise<T>) | ((self: any) => Promise<T>),
|
|
30
|
+
options: ResourceOptions<T> = {},
|
|
31
|
+
): FieldDecoratorReturn {
|
|
22
32
|
return createFieldDecorator({
|
|
23
|
-
bind(
|
|
24
|
-
const
|
|
33
|
+
bind(instance, _name, _initialValue): FieldBinding {
|
|
34
|
+
const bound =
|
|
35
|
+
fetcher.length > 0
|
|
36
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
37
|
+
? () => (fetcher as (self: any) => Promise<T>)(instance)
|
|
38
|
+
: (fetcher as () => Promise<T>);
|
|
39
|
+
const r = resource(bound, options);
|
|
25
40
|
return {
|
|
26
41
|
descriptor: {
|
|
27
42
|
get(): ResourceInstance<T> { return r; },
|
|
@@ -30,5 +45,5 @@ export function Resource<T>(fetcher: () => Promise<T>, options: ResourceOptions<
|
|
|
30
45
|
},
|
|
31
46
|
};
|
|
32
47
|
},
|
|
33
|
-
});
|
|
48
|
+
}) as unknown as FieldDecoratorReturn;
|
|
34
49
|
}
|