Skip to content

Commit 8ac9653

Browse files
Implement security features in OpenAPI
- Added support for various security schemes including API Key, HTTP Authentication, OAuth 2.0, OpenID Connect, and Mutual TLS. - Introduced global security requirements that can be applied to all endpoints. - Enhanced EndpointBuilder to allow setting security requirements at both global and operation levels. - Created comprehensive tests for security features, including integration tests for complete API scenarios. - Updated documentation to include detailed explanations of security mechanisms and best practices.
1 parent abb6308 commit 8ac9653

37 files changed

Lines changed: 2637 additions & 777 deletions

.github/workflows/publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ jobs:
1616
- uses: actions/checkout@v4
1717

1818
- name: Publish package
19-
run: npx jsr publish
19+
run: npx jsr publish

deno.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{
2+
"name": "@murat/openapi",
3+
"exports": "./mod.ts",
24
"tasks": {
35
"lint": "deno lint",
46
"test": "deno test --allow-all",

docs/README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
11
# OpenAPI Documentation
22

3-
Welcome to the documentation for the OpenAPI library. This package provides a developer-friendly way to define OpenAPI specifications for your APIs.
3+
Welcome to the documentation for the OpenAPI library. This package provides a
4+
developer-friendly way to define OpenAPI specifications for your APIs.
45

56
## Table of Contents
67

78
- [Getting Started](./getting-started.md)
89
- [EndpointBuilder](./endpoint-builder.md)
910
- [OpenAPI Schema](./openapi-schema.md)
11+
- [Security](./security.md)
12+
- [Components](./components.md)
1013
- [Examples](./examples.md)
1114

1215
## Overview
1316

14-
The OpenAPI library helps you define your API specifications in a structured way following the OpenAPI 3.1 standard. It provides:
17+
The OpenAPI library helps you define your API specifications in a structured way
18+
following the OpenAPI 3.1 standard. It provides:
1519

1620
- A fluent API for creating endpoints and their documentation
1721
- Type-safe schema definitions for request and response objects
1822
- Tools for serialization to JSON and YAML formats
1923
- Validation rule handling and documentation
24+
- Comprehensive security scheme definitions and requirements
2025

2126
## Basic Usage
2227

2328
Here's a basic example of creating an OpenAPI document:
2429

2530
```typescript
26-
import { OpenAPI, EndpointBuilder } from "@murat/openapi";
31+
import { EndpointBuilder, OpenAPI } from "@murat/openapi";
2732

2833
// Create a new OpenAPI document
2934
const api = new OpenAPI({
@@ -56,4 +61,5 @@ const jsonString = api.getJSONString();
5661
const yamlString = api.getYAMLString();
5762
```
5863

59-
For more detailed instructions, check out the [Getting Started](./getting-started.md) guide.
64+
For more detailed instructions, check out the
65+
[Getting Started](./getting-started.md) guide.

docs/components.md

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# OpenAPI Components
22

3-
Components are a powerful feature in OpenAPI 3.1 that allow you to define reusable objects in your API specification. The Components Object holds various schemas that can be referenced throughout the specification.
3+
Components are a powerful feature in OpenAPI 3.1 that allow you to define
4+
reusable objects in your API specification. The Components Object holds various
5+
schemas that can be referenced throughout the specification.
46

57
## Available Component Types
68

79
Yelix OpenAPI supports all component types from the OpenAPI 3.1 specification:
810

911
- **Schemas**: Reusable schema definitions
10-
- **Responses**: Reusable response objects
12+
- **Responses**: Reusable response objects
1113
- **Parameters**: Reusable parameter objects
1214
- **Examples**: Reusable examples
1315
- **RequestBodies**: Reusable request body objects
@@ -19,16 +21,17 @@ Yelix OpenAPI supports all component types from the OpenAPI 3.1 specification:
1921

2022
## Adding Components
2123

22-
All component methods return a reference object that can be used in other parts of your API specification.
24+
All component methods return a reference object that can be used in other parts
25+
of your API specification.
2326

2427
### Adding Schemas
2528

2629
```typescript
27-
import { OpenAPI } from "yelix/openapi";
30+
import { OpenAPI } from "jsr:@murat/openapi";
2831

2932
const openapi = new OpenAPI({
3033
title: "My API",
31-
version: "1.0.0"
34+
version: "1.0.0",
3235
});
3336

3437
// Define a reusable schema
@@ -37,9 +40,9 @@ const userSchemaRef = openapi.addSchema("User", {
3740
properties: {
3841
id: { type: "integer" },
3942
name: { type: "string" },
40-
email: { type: "string", format: "email" }
43+
email: { type: "string", format: "email" },
4144
},
42-
required: ["id", "name", "email"]
45+
required: ["id", "name", "email"],
4346
});
4447

4548
// Now you can reference this schema in your endpoints
@@ -59,12 +62,12 @@ const errorResponseRef = openapi.addResponse("Error", {
5962
type: "object",
6063
properties: {
6164
code: { type: "integer" },
62-
message: { type: "string" }
65+
message: { type: "string" },
6366
},
64-
required: ["code", "message"]
65-
}
66-
}
67-
}
67+
required: ["code", "message"],
68+
},
69+
},
70+
},
6871
});
6972
```
7073

@@ -81,8 +84,8 @@ const limitParamRef = openapi.addParameter("limit", {
8184
type: "integer",
8285
minimum: 1,
8386
maximum: 100,
84-
default: 20
85-
}
87+
default: 20,
88+
},
8689
});
8790
```
8891

@@ -94,7 +97,7 @@ const apiKeyRef = openapi.addSecurityScheme("ApiKey", {
9497
type: "apiKey",
9598
name: "X-API-KEY",
9699
in: "header",
97-
description: "API key authentication"
100+
description: "API key authentication",
98101
});
99102

100103
// Define OAuth2 security scheme
@@ -106,19 +109,20 @@ const oauth2Ref = openapi.addSecurityScheme("OAuth2", {
106109
tokenUrl: "https://example.com/oauth/token",
107110
scopes: {
108111
"read:items": "Read items",
109-
"write:items": "Write items"
110-
}
111-
}
112-
}
112+
"write:items": "Write items",
113+
},
114+
},
115+
},
113116
});
114117
```
115118

116119
## Using Components with EndpointBuilder
117120

118-
You can use the returned reference objects directly in your operations with EndpointBuilder:
121+
You can use the returned reference objects directly in your operations with
122+
EndpointBuilder:
119123

120124
```typescript
121-
import { EndpointBuilder } from "yelix/openapi";
125+
import { EndpointBuilder } from "jsr:@murat/openapi";
122126

123127
// Create an endpoint builder
124128
const getUserEndpoint = new EndpointBuilder()
@@ -131,16 +135,16 @@ const getUserEndpoint = new EndpointBuilder()
131135
in: "path",
132136
required: true,
133137
schema: {
134-
type: "integer"
135-
}
138+
type: "integer",
139+
},
136140
})
137141
.addResponse("200", {
138142
description: "User found",
139143
content: {
140144
"application/json": {
141-
schema: userSchemaRef // Using the reference from addSchema
142-
}
143-
}
145+
schema: userSchemaRef, // Using the reference from addSchema
146+
},
147+
},
144148
})
145149
.addResponse("404", errorResponseRef); // Using the reference from addResponse
146150

@@ -158,7 +162,12 @@ console.log(userSchema);
158162

159163
## Best Practices
160164

161-
1. **Use Components for Common Objects**: Define schemas, responses, and parameters that are used in multiple endpoints as components.
162-
2. **Consistent Naming**: Use a consistent naming convention for your components.
163-
3. **Schema Reuse**: Break down complex schemas into smaller, reusable components.
165+
1. **Use Components for Common Objects**: Define schemas, responses, and
166+
parameters that are used in multiple endpoints as components.
167+
2. **Consistent Naming**: Use a consistent naming convention for your
168+
components.
169+
3. **Schema Reuse**: Break down complex schemas into smaller, reusable
170+
components.
171+
172+
```
164173
```

0 commit comments

Comments
 (0)