sofa-api 0.11.2 → 0.12.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.
package/README.md CHANGED
@@ -20,47 +20,14 @@ Here's complete example with no dependency on frameworks, but also integratable
20
20
  ```js
21
21
  import http from 'http';
22
22
  import getStream from 'get-stream';
23
- import { createSofaRouter } from 'sofa-api';
24
-
25
- const invokeSofa = createSofaRouter({
26
- basePath: '/api',
27
- schema,
28
- });
23
+ import { useSofa } from 'sofa-api';
29
24
 
30
- const server = http.createServer(async (req, res) => {
31
- try {
32
- const response = await invokeSofa({
33
- method: req.method,
34
- url: req.url,
35
- body: JSON.parse(await getStream(req)),
36
- contextValue: {
37
- req,
38
- },
39
- });
40
- if (response) {
41
- const headers = {
42
- 'Content-Type': 'application/json',
43
- };
44
- if (response.statusMessage) {
45
- res.writeHead(response.status, response.statusMessage, headers);
46
- } else {
47
- res.writeHead(response.status, headers);
48
- }
49
- if (response.type === 'result') {
50
- res.end(JSON.stringify(response.body));
51
- }
52
- if (response.type === 'error') {
53
- res.end(JSON.stringify(response.error));
54
- }
55
- } else {
56
- res.writeHead(404);
57
- res.end();
58
- }
59
- } catch (error) {
60
- res.writeHead(500);
61
- res.end(JSON.stringify(error));
62
- }
63
- });
25
+ const server = http.createServer(
26
+ useSofa({
27
+ basePath: '/api',
28
+ schema,
29
+ })
30
+ );
64
31
  ```
65
32
 
66
33
  Another example with builtin express-like frameworks support
@@ -302,6 +269,7 @@ Thanks to GraphQL's Type System Sofa is able to generate OpenAPI (Swagger) defin
302
269
 
303
270
  ```ts
304
271
  import { useSofa, OpenAPI } from 'sofa-api';
272
+ import { writeFileSync } from 'fs';
305
273
 
306
274
  const openApi = OpenAPI({
307
275
  schema,
@@ -325,13 +293,14 @@ app.use(
325
293
  );
326
294
 
327
295
  // writes every recorder route
328
- openApi.save('./swagger.yml');
296
+ writeFileSync('./swagger.json', JSON.stringify(openApi.get(), null, 2));
329
297
  ```
330
298
 
331
299
  OpenAPI (Swagger) with Bearer Authentication
332
300
 
333
301
  ```ts
334
302
  import { useSofa, OpenAPI } from 'sofa-api';
303
+ import { writeFileSync } from 'fs';
335
304
 
336
305
  const openApi = OpenAPI({
337
306
  schema,
@@ -368,6 +337,66 @@ app.use(
368
337
  })
369
338
  );
370
339
 
340
+ // writes every recorder route
341
+ writeFileSync('./swagger.json', JSON.stringify(openApi.get(), null, 2));
342
+ ```
343
+
344
+ OpenAPI (Swagger) with custom tags, summary and description
345
+
346
+ ```ts
347
+ const openApi = OpenAPI({
348
+ schema,
349
+ info: {
350
+ title: 'Example API',
351
+ version: '3.0.0',
352
+ },
353
+ tags: [
354
+ {
355
+ name: 'Book',
356
+ description: 'Book related operations',
357
+ },
358
+ {
359
+ name: 'Author',
360
+ description: 'Author related operations',
361
+ },
362
+ ],
363
+ });
364
+ ```
365
+
366
+ ```ts
367
+ @Resolver(Book)
368
+ export class BookResolver {
369
+ @Query(() => Book, { description: 'Get book by id' }) // custom summary tag
370
+ getBookById(@Arg('id', () => Int) id: number) {
371
+ return 'book';
372
+ }
373
+ }
374
+ ```
375
+
376
+ ```ts
377
+ const routes: SofaConfig['routes'] = {
378
+ 'Query.getBookById': {
379
+ method: 'POST',
380
+ path: '/book/:id',
381
+ tags: ['Book'],
382
+ description: 'This is a custom detailed description for getBookById',
383
+ },
384
+ }
385
+
386
+ const createSofaMiddleware = (
387
+ schema: GraphQLSchema,
388
+ openApi: ReturnType<typeof OpenAPI>,
389
+ basePath = ''
390
+ ): ReturnType<typeof useSofa> => {
391
+ return useSofa({
392
+ routes,
393
+ basePath,
394
+ schema,
395
+ onRoute(info) {
396
+ openApi.addRoute(info, { basePath });
397
+ },
398
+ });
399
+ };
371
400
  // writes every recorder route
372
401
  openApi.save('./swagger.yml');
373
402
  ```
package/index.d.ts CHANGED
@@ -1,38 +1,3 @@
1
- /// <reference types="node" />
2
- import * as http from 'http';
3
- import type { ContextValue } from './types';
4
1
  import type { SofaConfig } from './sofa';
5
2
  export { OpenAPI } from './open-api';
6
- declare type Request = http.IncomingMessage & {
7
- method: string;
8
- url: string;
9
- originalUrl?: string;
10
- body?: any;
11
- };
12
- declare type NextFunction = (err?: any) => void;
13
- declare type Middleware = (req: Request, res: http.ServerResponse, next: NextFunction) => unknown;
14
- export declare type ContextFn = (init: {
15
- req: any;
16
- res: any;
17
- }) => ContextValue;
18
- export declare function isContextFn(context: any): context is ContextFn;
19
- interface SofaMiddlewareConfig extends SofaConfig {
20
- context?: ContextValue | ContextFn;
21
- }
22
- export declare function useSofa({ context, ...config }: SofaMiddlewareConfig): Middleware;
23
- export declare function createSofaRouter(config: SofaConfig): (request: {
24
- method: string;
25
- url: string;
26
- body: any;
27
- contextValue: ContextValue;
28
- }) => Promise<({
29
- type: "result";
30
- status: number;
31
- statusMessage?: string | undefined;
32
- body: any;
33
- } | {
34
- type: "error";
35
- status: number;
36
- statusMessage?: string | undefined;
37
- error: any;
38
- }) | null>;
3
+ export declare function useSofa(config: SofaConfig): import("@whatwg-node/server").ServerAdapter<unknown, import("itty-router").Router<import("itty-router").Request & Request, {}>>;