ts-forge 1.0.0 → 1.0.1-beta.0

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.
Files changed (3) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/README.md +158 -1
  3. package/package.json +7 -9
package/CHANGELOG.md CHANGED
@@ -1,4 +1,11 @@
1
+ ## v1.0.1-beta.0
2
+
3
+ - README.md: documentation finished
4
+ - Updated dev dependencies
5
+ - Improved type definitions for getDefinitionsForClass function
6
+
1
7
  ## v1.0.0
2
8
 
3
9
  - `@Resolver()` and `@ResolverFn()` decorators were created
4
10
  - Created `getDefinitionsForClass()` function which handles resolver functions definition
11
+ - Unit testing for decorators and utils
package/README.md CHANGED
@@ -1,5 +1,67 @@
1
1
  **Warning:** The first version of this library is ready, however documentation is still in progress
2
2
 
3
+ This library allows you to define resolvers for your Atlassian Forge addon using decorators. It uses the `@forge/resolver` library under the hood to define resolvers, but it provides a more convenient way to define them using decorators.
4
+
5
+ ## Table of contents
6
+
7
+ - [Introduction](#introduction)
8
+ - [Table of Contents](#table-of-contents)
9
+ - [Installation](#installation)
10
+ - [@Resolver](#resolver)
11
+ - [@ResolverFn](#resolverfn)
12
+ - [getDefinitionsForClass](#getdefinitionsforclass)
13
+ - [Middlewares](#middlewares)
14
+ - [Error handler](#error-handler)
15
+
16
+ [![npm version](https://badge.fury.io/js/ts-forge.svg)](https://badge.fury.io/js/ts-forge)
17
+ [![npm downloads](https://img.shields.io/npm/dm/ts-forge.svg)](https://www.npmjs.com/package/ts-forge)
18
+
19
+ ## Introduction
20
+
21
+ This library is designed to make it easier to define resolvers in your Atlassian Forge addon using TypeScript decorators. It allows you to define resolvers, middlewares, and error handlers in a more structured and readable way, while still leveraging the power of the `@forge/resolver` library.
22
+
23
+ It provides two main decorators: `@Resolver` and `@ResolverFn`, which can be used to define resolvers and their configurations. Additionally, it provides a function `getDefinitionsForClass` to get the definitions of the resolvers you created.
24
+
25
+ When you create a resolver, you typically would do something like this:
26
+
27
+ ```ts
28
+ import Resolver "@forge/resolver";
29
+
30
+ const resolver = new Resolver();
31
+
32
+ resolver.define("hello", async (req) => {
33
+ try {
34
+ return { message: "Hello world!" };
35
+ } catch(error) {
36
+ console.error("An error occurred:", error);
37
+
38
+ return { status: "error", message: "An error occurred" };
39
+ }
40
+ });
41
+
42
+ resolver.define("update-user", async (req) => {
43
+ try {
44
+ // Update user logic...
45
+
46
+ return { status: "success", message: "User updated successfully" };
47
+ } catch(error) {
48
+ console.error("An error occurred:", error);
49
+
50
+ return { status: "error", message: "An error occurred" };
51
+ }
52
+ });
53
+
54
+ export const definitions = resolver.getDefinitions();
55
+ ```
56
+
57
+ ## Installation
58
+
59
+ This is a Node.js module available on npm. Make sure you have `@forge/resolver` installed as well, since this library uses it under the hood. The minimum version of `@forge/resolver` required is `^1.6.0`.
60
+
61
+ ```bash
62
+ npm install ts-forge
63
+ ```
64
+
3
65
  ## @Resolver
4
66
 
5
67
  ```ts
@@ -225,4 +287,99 @@ const definitions = getDefinitionsForClass({
225
287
 
226
288
  ## Error handler
227
289
 
228
- An error handler is a function that is called when an error is thrown in a resolver function
290
+ An error handler is a function that allows you to handle errors thrown in your resolver functions or middlewares. It can be defined in `@ResolverFn()`, `@Resolver()` and `getDefinitionsForClass()`.
291
+
292
+ When an error is thrown, the error handler will be called with the error and the request object, allowing you to handle the error and return a response to the frontend.
293
+
294
+ When an error is thrown, the error handler will be called in the following order:
295
+
296
+ 1. If an error handler is defined in the `@ResolverFn()` decorator, it will be called
297
+ 2. Error handler defined in `@Resolver()`, will be called when an error handler was not defined in `@ResolverFn()`
298
+ 3. If no error handler was defined in `@ResolverFn()` and `@Resolver()`, the error handler defined in `getDefinitionsForClass()` will be called
299
+ 4. If no error handler was defined in any of the above, the error will be logged to the console and will be returned to the frontend
300
+
301
+ #### Usage
302
+
303
+ This is an example of an error handler function
304
+
305
+ ```ts
306
+ import { Request } from "@forge/resolver";
307
+
308
+ function myErrorHandler(error: any, request: Request): any | Promise<any> {
309
+ // Handle the error and return a response
310
+ console.error("An error occurred:", error);
311
+
312
+ return { status: "error", message: "An error occurred" };
313
+ }
314
+ ```
315
+
316
+ Then you can use this error handler in `@ResolverFn()`, `@Resolver()` or `getDefinitionsForClass()`
317
+ In the following example the `myErrorHandler` error handler is used in `@ResolverFn()`. This is useful when you want certain error handler to be called for some resolver functions
318
+
319
+ ```ts
320
+ @Resolver()
321
+ class HelloWorldResolver {
322
+ @ResolverFn({
323
+ key: "hello-world",
324
+ errorHandler: myErrorHandler
325
+ })
326
+ async helloWorld() {
327
+ // Do whatever this resolver should do
328
+ }
329
+
330
+ // myErrorHandler won't be called when an error is thrown in this resolver function
331
+ @ResolverFn("get-current-user")
332
+ async getCurrentUser() {
333
+ // Return current user
334
+ }
335
+ }
336
+ ```
337
+
338
+ In this example the `myErrorHandler` error handler is used in `@Resolver()`. Error handlers defined in this decorator will be called for all resolver functions defined in the class.
339
+
340
+ ```ts
341
+ @Resolver({
342
+ // Error handler defined in this function will be called if an error is thrown in any of the resolver functions defined in this class
343
+ errorHandler: myErrorHandler
344
+ })
345
+ class HelloWorldResolver {
346
+ // There's no need to add the same error handler in @ResolverFn()
347
+ @ResolverFn("hello-world")
348
+ async helloWorld() {
349
+ // Do whatever this resolver should do
350
+ }
351
+
352
+ // myErrorHandler will also be called if an error is thrown in this resolver function
353
+ @ResolverFn("get-current-user")
354
+ async getCurrentUser() {
355
+ // Return current user
356
+ }
357
+ }
358
+ ```
359
+
360
+ Last but not least, you can define an error handler in the `getDefinitionsForClass` function. Error handlers defined here will be called if an error is thrown in any of the resolvers that you pass to the `resolvers` array.
361
+
362
+ ```ts
363
+ @Resolver()
364
+ class HelloWorldResolver {
365
+ @ResolverFn("hello-world")
366
+ helloWorld(request: Request) {
367
+ return { message: "Hello world!" };
368
+ }
369
+ }
370
+
371
+ @Resolver()
372
+ class UserResolver {
373
+ @ResolverFn("get-user")
374
+ getUser(request: Request) {
375
+ return {...};
376
+ }
377
+ }
378
+
379
+ const definitions = getDefinitionsForClass({
380
+ errorHandler: myErrorHandler,
381
+ // Error handler will be called if an error is thrown in any of the resolver functions
382
+ // You don't need to add the myErrorHandler error handler in @Resolver or @ResolverFn decorators
383
+ resolvers: [HelloWorldResolver, UserResolver]
384
+ });
385
+ ```
package/package.json CHANGED
@@ -1,12 +1,10 @@
1
1
  {
2
2
  "name": "ts-forge",
3
- "version": "1.0.0",
3
+ "version": "1.0.1-beta.0",
4
4
  "main": "dist/index.js",
5
5
  "scripts": {
6
- "test": "vitest --run",
7
- "test:ui": "vitest --ui",
8
- "test:watch": "vitest",
9
- "test:coverage": "vitest --run --coverage",
6
+ "test": "vitest --run --coverage",
7
+ "test:ui": "vitest --ui --coverage",
10
8
  "dev": "tsc -p ./tsconfig.json --watch",
11
9
  "build": "eval $(rm -r dist) && tsc -p ./tsconfig.json",
12
10
  "prepublish": "npm run build"
@@ -27,9 +25,9 @@
27
25
  "@forge/resolver": ">=1.6.0 <2.0.0"
28
26
  },
29
27
  "devDependencies": {
30
- "@vitest/coverage-v8": "3.1.4",
31
- "jsdom": "^26.1.0",
32
- "typescript": "5.8.3",
33
- "vitest": "^3.1.4"
28
+ "@vitest/coverage-v8": "4.0.8",
29
+ "jsdom": "^27.1.0",
30
+ "typescript": "5.9.3",
31
+ "vitest": "^4.0.8"
34
32
  }
35
33
  }