Skip to content

Commit 431e513

Browse files
authored
feat(server): Audits and test function (#4)
1 parent b0b6642 commit 431e513

File tree

14 files changed

+953
-449
lines changed

14 files changed

+953
-449
lines changed

README.md

+26
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,32 @@ console.log('Listening to port 4000');
709709

710710
</details>
711711

712+
<details id="audit-jest">
713+
<summary><a href="#audit-jest">🔗</a> Audit for servers usage in <a href="https://jestjs.io">Jest</a> environment</summary>
714+
715+
```js
716+
import { fetch } from '@whatwg-node/fetch';
717+
import { serverAudits } from 'graphql-http';
718+
719+
for (const audit of serverAudits({
720+
url: 'http://localhost:4000/graphql',
721+
fetchFn: fetch,
722+
})) {
723+
test(audit.name, async () => {
724+
const result = await audit.fn();
725+
if (result.status === 'error') {
726+
throw result.reason;
727+
}
728+
if (result.status === 'warn') {
729+
console.warn(result.reason); // or throw if you want full compliance (warnings are not requirements)
730+
}
731+
// result.status === 'ok'
732+
});
733+
}
734+
```
735+
736+
</details>
737+
712738
## [Documentation](docs/)
713739

714740
Check the [docs folder](docs/) out for [TypeDoc](https://typedoc.org) generated documentation.

docs/README.md

+71
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ graphql-http
1010

1111
### Interfaces
1212

13+
- [Audit](interfaces/Audit.md)
14+
- [AuditFail](interfaces/AuditFail.md)
15+
- [AuditOk](interfaces/AuditOk.md)
1316
- [Client](interfaces/Client.md)
1417
- [ClientOptions](interfaces/ClientOptions.md)
1518
- [HandlerOptions](interfaces/HandlerOptions.md)
@@ -18,11 +21,15 @@ graphql-http
1821
- [RequestParams](interfaces/RequestParams.md)
1922
- [ResponseInit](interfaces/ResponseInit.md)
2023
- [ResponseLike](interfaces/ResponseLike.md)
24+
- [ServerAuditOptions](interfaces/ServerAuditOptions.md)
2125
- [Sink](interfaces/Sink.md)
2226

2327
### Type Aliases
2428

2529
- [AcceptableMediaType](README.md#acceptablemediatype)
30+
- [AuditName](README.md#auditname)
31+
- [AuditRequirement](README.md#auditrequirement)
32+
- [AuditResult](README.md#auditresult)
2633
- [ExecutionContext](README.md#executioncontext)
2734
- [Handler](README.md#handler)
2835
- [Response](README.md#response)
@@ -31,11 +38,75 @@ graphql-http
3138

3239
### Functions
3340

41+
- [auditServer](README.md#auditserver)
3442
- [createClient](README.md#createclient)
3543
- [createHandler](README.md#createhandler)
3644
- [getAcceptableMediaType](README.md#getacceptablemediatype)
3745
- [isResponse](README.md#isresponse)
3846
- [makeResponse](README.md#makeresponse)
47+
- [serverAudits](README.md#serveraudits)
48+
49+
## Audits
50+
51+
### AuditName
52+
53+
Ƭ **AuditName**: \`${AuditRequirement} ${string}\`
54+
55+
Audit name starting with the audit requirement level.
56+
57+
___
58+
59+
### AuditRequirement
60+
61+
Ƭ **AuditRequirement**: ``"MUST"`` \| ``"SHOULD"`` \| ``"MAY"``
62+
63+
Audit requirement levels as per [RFC2119](https://www.rfc-editor.org/rfc/rfc2119).
64+
65+
___
66+
67+
### AuditResult
68+
69+
Ƭ **AuditResult**: [`AuditOk`](interfaces/AuditOk.md) \| [`AuditFail`](interfaces/AuditFail.md)
70+
71+
Result of the performed audit. See `AuditOk` and `AuditFail` for more information.
72+
73+
___
74+
75+
### auditServer
76+
77+
**auditServer**(`opts`): `Promise`<[`AuditResult`](README.md#auditresult)[]\>
78+
79+
Performs the full list of server audits required for GraphQL over HTTP spec conformance.
80+
81+
Please consult the `AuditResult` for more information.
82+
83+
#### Parameters
84+
85+
| Name | Type |
86+
| :------ | :------ |
87+
| `opts` | [`ServerAuditOptions`](interfaces/ServerAuditOptions.md) |
88+
89+
#### Returns
90+
91+
`Promise`<[`AuditResult`](README.md#auditresult)[]\>
92+
93+
___
94+
95+
### serverAudits
96+
97+
**serverAudits**(`opts`): [`Audit`](interfaces/Audit.md)[]
98+
99+
List of server audits required to check GraphQL over HTTP spec conformance.
100+
101+
#### Parameters
102+
103+
| Name | Type |
104+
| :------ | :------ |
105+
| `opts` | [`ServerAuditOptions`](interfaces/ServerAuditOptions.md) |
106+
107+
#### Returns
108+
109+
[`Audit`](interfaces/Audit.md)[]
39110

40111
## Client
41112

docs/interfaces/Audit.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[graphql-http](../README.md) / Audit
2+
3+
# Interface: Audit
4+
5+
Actual audit test returning an result.
6+
7+
The test function will throw only if the error is fatal.
8+
9+
## Table of contents
10+
11+
### Properties
12+
13+
- [fn](Audit.md#fn)
14+
- [name](Audit.md#name)
15+
16+
## Properties
17+
18+
### fn
19+
20+
**fn**: () => `Promise`<[`AuditResult`](../README.md#auditresult)\>
21+
22+
#### Type declaration
23+
24+
▸ (): `Promise`<[`AuditResult`](../README.md#auditresult)\>
25+
26+
##### Returns
27+
28+
`Promise`<[`AuditResult`](../README.md#auditresult)\>
29+
30+
___
31+
32+
### name
33+
34+
**name**: \`MUST ${string}\` \| \`SHOULD ${string}\` \| \`MAY ${string}\`

docs/interfaces/AuditFail.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[graphql-http](../README.md) / AuditFail
2+
3+
# Interface: AuditFail
4+
5+
Indicates that the audit failed.
6+
7+
If the status is `warn`, the audit is not a requirement but rather a recommendation.
8+
9+
On the other hand, if the status is `error`, the audit is a requirement and the source
10+
is therefore not compliant.
11+
12+
## Table of contents
13+
14+
### Properties
15+
16+
- [name](AuditFail.md#name)
17+
- [reason](AuditFail.md#reason)
18+
- [status](AuditFail.md#status)
19+
20+
## Properties
21+
22+
### name
23+
24+
**name**: \`MUST ${string}\` \| \`SHOULD ${string}\` \| \`MAY ${string}\`
25+
26+
___
27+
28+
### reason
29+
30+
**reason**: `string`
31+
32+
___
33+
34+
### status
35+
36+
**status**: ``"warn"`` \| ``"error"``

docs/interfaces/AuditOk.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[graphql-http](../README.md) / AuditOk
2+
3+
# Interface: AuditOk
4+
5+
Indicates that the audit was successful.
6+
7+
## Table of contents
8+
9+
### Properties
10+
11+
- [name](AuditOk.md#name)
12+
- [status](AuditOk.md#status)
13+
14+
## Properties
15+
16+
### name
17+
18+
**name**: \`MUST ${string}\` \| \`SHOULD ${string}\` \| \`MAY ${string}\`
19+
20+
___
21+
22+
### status
23+
24+
**status**: ``"ok"``

docs/interfaces/ServerAuditOptions.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[graphql-http](../README.md) / ServerAuditOptions
2+
3+
# Interface: ServerAuditOptions
4+
5+
Options for server audits required to check GraphQL over HTTP spec conformance.
6+
7+
## Table of contents
8+
9+
### Properties
10+
11+
- [fetchFn](ServerAuditOptions.md#fetchfn)
12+
- [url](ServerAuditOptions.md#url)
13+
14+
## Properties
15+
16+
### fetchFn
17+
18+
`Optional` **fetchFn**: `unknown`
19+
20+
The Fetch function to use.
21+
22+
For NodeJS environments consider using [`@whatwg-node/fetch`](https://github.com/ardatan/whatwg-node/tree/master/packages/fetch).
23+
24+
**`Default`**
25+
26+
global.fetch
27+
28+
___
29+
30+
### url
31+
32+
**url**: `string`
33+
34+
The URL of the GraphQL server for the audit.

0 commit comments

Comments
 (0)