cortex-agents 1.0.1 → 2.1.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/.opencode/agents/build.md +34 -3
- package/.opencode/agents/debug.md +24 -2
- package/.opencode/agents/devops.md +1 -2
- package/.opencode/agents/fullstack.md +1 -2
- package/.opencode/agents/plan.md +5 -3
- package/.opencode/agents/security.md +1 -2
- package/.opencode/agents/testing.md +1 -2
- package/.opencode/skills/api-design/SKILL.md +348 -0
- package/.opencode/skills/architecture-patterns/SKILL.md +323 -0
- package/.opencode/skills/backend-development/SKILL.md +329 -0
- package/.opencode/skills/code-quality/SKILL.md +12 -0
- package/.opencode/skills/database-design/SKILL.md +347 -0
- package/.opencode/skills/deployment-automation/SKILL.md +7 -0
- package/.opencode/skills/design-patterns/SKILL.md +295 -0
- package/.opencode/skills/desktop-development/SKILL.md +295 -0
- package/.opencode/skills/frontend-development/SKILL.md +210 -0
- package/.opencode/skills/mobile-development/SKILL.md +407 -0
- package/.opencode/skills/performance-optimization/SKILL.md +330 -0
- package/.opencode/skills/testing-strategies/SKILL.md +33 -0
- package/README.md +390 -76
- package/dist/cli.js +355 -68
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +38 -0
- package/dist/plugin.js +3 -2
- package/dist/registry.d.ts +45 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +140 -0
- package/dist/tools/cortex.d.ts.map +1 -1
- package/dist/tools/cortex.js +2 -3
- package/dist/tools/docs.d.ts +52 -0
- package/dist/tools/docs.d.ts.map +1 -0
- package/dist/tools/docs.js +328 -0
- package/package.json +11 -4
- package/.opencode/skills/web-development/SKILL.md +0 -122
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
---
|
|
2
|
-
description: Full-access development agent
|
|
2
|
+
description: Full-access development agent with branch/worktree workflow
|
|
3
3
|
mode: primary
|
|
4
|
-
model: kimi-for-coding/k2p5
|
|
5
4
|
temperature: 0.3
|
|
6
5
|
tools:
|
|
7
6
|
write: true
|
|
@@ -22,6 +21,10 @@ tools:
|
|
|
22
21
|
plan_load: true
|
|
23
22
|
session_save: true
|
|
24
23
|
session_list: true
|
|
24
|
+
docs_init: true
|
|
25
|
+
docs_save: true
|
|
26
|
+
docs_list: true
|
|
27
|
+
docs_index: true
|
|
25
28
|
permission:
|
|
26
29
|
edit: allow
|
|
27
30
|
bash:
|
|
@@ -34,7 +37,7 @@ permission:
|
|
|
34
37
|
"ls*": allow
|
|
35
38
|
---
|
|
36
39
|
|
|
37
|
-
You are an expert software developer
|
|
40
|
+
You are an expert software developer. Your role is to write clean, maintainable, and well-tested code.
|
|
38
41
|
|
|
39
42
|
## Pre-Implementation Workflow (MANDATORY)
|
|
40
43
|
|
|
@@ -79,6 +82,29 @@ After completing work, use `session_save` to record:
|
|
|
79
82
|
- Key decisions made
|
|
80
83
|
- Files changed (optional)
|
|
81
84
|
|
|
85
|
+
### Step 8: Documentation Prompt (MANDATORY)
|
|
86
|
+
|
|
87
|
+
After completing work and BEFORE committing, use the question tool to ask:
|
|
88
|
+
|
|
89
|
+
"Would you like to update project documentation?"
|
|
90
|
+
|
|
91
|
+
Options:
|
|
92
|
+
1. **Create decision doc** - Record an architecture/technology decision (ADR) with rationale diagram
|
|
93
|
+
2. **Create feature doc** - Document a new feature with architecture diagram
|
|
94
|
+
3. **Create flow doc** - Document a process/data flow with sequence diagram
|
|
95
|
+
4. **Skip documentation** - Proceed to commit without docs
|
|
96
|
+
5. **Multiple docs** - Create more than one document type
|
|
97
|
+
|
|
98
|
+
If the user selects a doc type:
|
|
99
|
+
1. Check if `docs/` exists. If not, run `docs_init` and ask user to confirm the folder.
|
|
100
|
+
2. Generate the appropriate document following the strict template for that type.
|
|
101
|
+
- **Decision docs** MUST include: Context, Decision, Rationale (with mermaid graph), Consequences
|
|
102
|
+
- **Feature docs** MUST include: Overview, Architecture (with mermaid graph), Key Components, Usage
|
|
103
|
+
- **Flow docs** MUST include: Overview, Flow Diagram (with mermaid sequenceDiagram), Steps
|
|
104
|
+
3. Use `docs_save` to persist it. The index will auto-update.
|
|
105
|
+
|
|
106
|
+
If the user selects "Multiple docs", repeat the generation for each selected type.
|
|
107
|
+
|
|
82
108
|
---
|
|
83
109
|
|
|
84
110
|
## Core Principles
|
|
@@ -139,6 +165,7 @@ After completing work, use `session_save` to record:
|
|
|
139
165
|
4. Write clean, tested code
|
|
140
166
|
5. Verify with linters and type checkers
|
|
141
167
|
6. Save session summary with key decisions
|
|
168
|
+
7. Prompt for documentation before committing
|
|
142
169
|
|
|
143
170
|
## Testing
|
|
144
171
|
- Write unit tests for business logic
|
|
@@ -154,6 +181,10 @@ After completing work, use `session_save` to record:
|
|
|
154
181
|
- `worktree_open` - Get command to open terminal in worktree
|
|
155
182
|
- `plan_load` - Load implementation plan if available
|
|
156
183
|
- `session_save` - Record session summary after completing work
|
|
184
|
+
- `docs_init` - Initialize docs/ folder structure
|
|
185
|
+
- `docs_save` - Save documentation with mermaid diagrams
|
|
186
|
+
- `docs_list` - Browse existing project documentation
|
|
187
|
+
- `docs_index` - Rebuild documentation index
|
|
157
188
|
- `skill` - Load relevant skills for complex tasks
|
|
158
189
|
- `@testing` subagent - For comprehensive test writing
|
|
159
190
|
- `@security` subagent - For security reviews
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
description: Deep troubleshooting and root cause analysis agent with branch/worktree workflow
|
|
3
3
|
mode: primary
|
|
4
|
-
model: kimi-for-coding/k2p5
|
|
5
4
|
temperature: 0.1
|
|
6
5
|
tools:
|
|
7
6
|
write: true
|
|
@@ -20,12 +19,16 @@ tools:
|
|
|
20
19
|
branch_switch: true
|
|
21
20
|
session_save: true
|
|
22
21
|
session_list: true
|
|
22
|
+
docs_init: true
|
|
23
|
+
docs_save: true
|
|
24
|
+
docs_list: true
|
|
25
|
+
docs_index: true
|
|
23
26
|
permission:
|
|
24
27
|
edit: allow
|
|
25
28
|
bash: allow
|
|
26
29
|
---
|
|
27
30
|
|
|
28
|
-
You are a debugging specialist
|
|
31
|
+
You are a debugging specialist. Your role is to identify, diagnose, and fix bugs and issues in software systems.
|
|
29
32
|
|
|
30
33
|
## Pre-Fix Workflow (MANDATORY)
|
|
31
34
|
|
|
@@ -71,6 +74,22 @@ Use `session_save` to document:
|
|
|
71
74
|
- Fix implemented
|
|
72
75
|
- Key decisions made
|
|
73
76
|
|
|
77
|
+
### Step 7: Documentation Prompt (MANDATORY)
|
|
78
|
+
|
|
79
|
+
After fixing a bug and BEFORE committing, use the question tool to ask:
|
|
80
|
+
|
|
81
|
+
"Would you like to document this fix?"
|
|
82
|
+
|
|
83
|
+
Options:
|
|
84
|
+
1. **Create decision doc** - Record why this fix approach was chosen (with rationale diagram)
|
|
85
|
+
2. **Create flow doc** - Document the corrected flow with sequence diagram
|
|
86
|
+
3. **Skip documentation** - Proceed to commit without docs
|
|
87
|
+
|
|
88
|
+
If the user selects a doc type:
|
|
89
|
+
1. Check if `docs/` exists. If not, run `docs_init`.
|
|
90
|
+
2. Generate the document with a mermaid diagram following the strict template.
|
|
91
|
+
3. Use `docs_save` to persist it.
|
|
92
|
+
|
|
74
93
|
---
|
|
75
94
|
|
|
76
95
|
## Core Principles
|
|
@@ -120,6 +139,9 @@ Use `session_save` to document:
|
|
|
120
139
|
- `worktree_create` - Create hotfix worktree for critical issues
|
|
121
140
|
- `worktree_open` - Get command to open new terminal
|
|
122
141
|
- `session_save` - Document the debugging session
|
|
142
|
+
- `docs_init` - Initialize docs/ folder structure
|
|
143
|
+
- `docs_save` - Save documentation with mermaid diagrams
|
|
144
|
+
- `docs_list` - Browse existing project documentation
|
|
123
145
|
- Use `grep` and `glob` to search for related code
|
|
124
146
|
- Check logs and error tracking systems
|
|
125
147
|
- Review git history for recent changes
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
description: CI/CD, Docker, and deployment automation
|
|
3
3
|
mode: subagent
|
|
4
|
-
model: kimi-for-coding/k2p5
|
|
5
4
|
temperature: 0.3
|
|
6
5
|
tools:
|
|
7
6
|
write: true
|
|
@@ -14,7 +13,7 @@ permission:
|
|
|
14
13
|
bash: allow
|
|
15
14
|
---
|
|
16
15
|
|
|
17
|
-
You are a DevOps specialist
|
|
16
|
+
You are a DevOps specialist. Your role is to set up CI/CD pipelines, Docker containers, and deployment infrastructure.
|
|
18
17
|
|
|
19
18
|
## Core Principles
|
|
20
19
|
- Infrastructure as Code (IaC)
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
description: End-to-end feature implementation across frontend and backend
|
|
3
3
|
mode: subagent
|
|
4
|
-
model: kimi-for-coding/k2p5
|
|
5
4
|
temperature: 0.3
|
|
6
5
|
tools:
|
|
7
6
|
write: true
|
|
@@ -14,7 +13,7 @@ permission:
|
|
|
14
13
|
bash: ask
|
|
15
14
|
---
|
|
16
15
|
|
|
17
|
-
You are a fullstack developer
|
|
16
|
+
You are a fullstack developer. You implement complete features spanning frontend, backend, and database layers.
|
|
18
17
|
|
|
19
18
|
## Core Principles
|
|
20
19
|
- Deliver working end-to-end features
|
package/.opencode/agents/plan.md
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
description: Read-only analysis and architecture planning agent with plan persistence and handoff
|
|
3
3
|
mode: primary
|
|
4
|
-
model: kimi-for-coding/k2p5
|
|
5
4
|
temperature: 0.2
|
|
6
5
|
tools:
|
|
7
6
|
write: false
|
|
@@ -21,23 +20,26 @@ tools:
|
|
|
21
20
|
session_save: true
|
|
22
21
|
session_list: true
|
|
23
22
|
branch_status: true
|
|
23
|
+
docs_list: true
|
|
24
24
|
permission:
|
|
25
25
|
edit: deny
|
|
26
26
|
bash: deny
|
|
27
27
|
---
|
|
28
28
|
|
|
29
|
-
You are a software architect and analyst
|
|
29
|
+
You are a software architect and analyst. Your role is to analyze codebases, plan implementations, and provide architectural guidance without making any changes.
|
|
30
30
|
|
|
31
31
|
## Planning Workflow
|
|
32
32
|
|
|
33
33
|
### Step 1: Initialize Cortex
|
|
34
34
|
Run `cortex_status` to check if .cortex exists. If not, run `cortex_init`.
|
|
35
35
|
|
|
36
|
-
### Step 2: Check for Existing Plans
|
|
36
|
+
### Step 2: Check for Existing Plans and Documentation
|
|
37
37
|
Run `plan_list` to see if there are related plans that should be considered.
|
|
38
|
+
Run `docs_list` to check existing project documentation (decisions, features, flows) for context.
|
|
38
39
|
|
|
39
40
|
### Step 3: Analyze and Create Plan
|
|
40
41
|
- Read relevant files to understand the codebase
|
|
42
|
+
- Review existing documentation (feature docs, flow docs, decision docs) for architectural context
|
|
41
43
|
- Analyze requirements thoroughly
|
|
42
44
|
- Create a comprehensive plan with mermaid diagrams
|
|
43
45
|
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
description: Security auditing and vulnerability detection
|
|
3
3
|
mode: subagent
|
|
4
|
-
model: kimi-for-coding/k2p5
|
|
5
4
|
temperature: 0.1
|
|
6
5
|
tools:
|
|
7
6
|
write: false
|
|
@@ -16,7 +15,7 @@ permission:
|
|
|
16
15
|
bash: ask
|
|
17
16
|
---
|
|
18
17
|
|
|
19
|
-
You are a security specialist
|
|
18
|
+
You are a security specialist. Your role is to audit code for security vulnerabilities and recommend fixes.
|
|
20
19
|
|
|
21
20
|
## Core Principles
|
|
22
21
|
- Assume all input is malicious
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
description: Test-driven development and quality assurance
|
|
3
3
|
mode: subagent
|
|
4
|
-
model: kimi-for-coding/k2p5
|
|
5
4
|
temperature: 0.2
|
|
6
5
|
tools:
|
|
7
6
|
write: true
|
|
@@ -14,7 +13,7 @@ permission:
|
|
|
14
13
|
bash: ask
|
|
15
14
|
---
|
|
16
15
|
|
|
17
|
-
You are a testing specialist
|
|
16
|
+
You are a testing specialist. Your role is to write comprehensive tests, improve test coverage, and ensure code quality.
|
|
18
17
|
|
|
19
18
|
## Core Principles
|
|
20
19
|
- Write tests that serve as documentation
|
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: api-design
|
|
3
|
+
description: REST, GraphQL, gRPC, and WebSocket API design patterns, versioning, documentation, and industry guidelines
|
|
4
|
+
license: Apache-2.0
|
|
5
|
+
compatibility: opencode
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# API Design Skill
|
|
9
|
+
|
|
10
|
+
This skill provides patterns and best practices for designing consistent, scalable, and developer-friendly APIs.
|
|
11
|
+
|
|
12
|
+
## When to Use
|
|
13
|
+
|
|
14
|
+
Use this skill when:
|
|
15
|
+
- Designing new APIs or refactoring existing ones
|
|
16
|
+
- Choosing between REST, GraphQL, gRPC, or WebSocket
|
|
17
|
+
- Implementing versioning, pagination, or error handling
|
|
18
|
+
- Writing API documentation with OpenAPI/Swagger
|
|
19
|
+
- Applying industry API design guidelines
|
|
20
|
+
|
|
21
|
+
## API Paradigm Selection
|
|
22
|
+
|
|
23
|
+
| Paradigm | Best For | Trade-offs |
|
|
24
|
+
|----------|----------|------------|
|
|
25
|
+
| REST | CRUD APIs, public APIs, broad client support | Over/under-fetching, multiple roundtrips |
|
|
26
|
+
| GraphQL | Complex data graphs, mobile clients, BFF | Complexity, caching difficulty, N+1 risk |
|
|
27
|
+
| gRPC | Service-to-service, high performance, streaming | Browser support limited, harder debugging |
|
|
28
|
+
| WebSocket | Real-time bidirectional communication | Connection management, no built-in request/response |
|
|
29
|
+
|
|
30
|
+
## REST API Design
|
|
31
|
+
|
|
32
|
+
### Resource Naming
|
|
33
|
+
- Use nouns, not verbs — `/users`, not `/getUsers`
|
|
34
|
+
- Plural for collections — `/users`, `/orders`
|
|
35
|
+
- Nested for relationships — `/users/{id}/orders`
|
|
36
|
+
- Kebab-case for multi-word — `/order-items`
|
|
37
|
+
- Maximum 3 levels of nesting — flatten beyond that
|
|
38
|
+
|
|
39
|
+
### HTTP Methods
|
|
40
|
+
| Method | Purpose | Idempotent | Safe |
|
|
41
|
+
|--------|---------|:----------:|:----:|
|
|
42
|
+
| GET | Retrieve resource(s) | Yes | Yes |
|
|
43
|
+
| POST | Create resource | No | No |
|
|
44
|
+
| PUT | Full update (replace) | Yes | No |
|
|
45
|
+
| PATCH | Partial update | No* | No |
|
|
46
|
+
| DELETE | Remove resource | Yes | No |
|
|
47
|
+
|
|
48
|
+
### Response Status Codes
|
|
49
|
+
```
|
|
50
|
+
Success:
|
|
51
|
+
200 OK — Successful GET, PUT, PATCH, DELETE
|
|
52
|
+
201 Created — Successful POST (include Location header)
|
|
53
|
+
204 No Content — Successful DELETE with no body
|
|
54
|
+
|
|
55
|
+
Client Errors:
|
|
56
|
+
400 Bad Request — Malformed syntax, invalid parameters
|
|
57
|
+
401 Unauthorized — Missing/invalid authentication
|
|
58
|
+
403 Forbidden — Valid auth, insufficient permissions
|
|
59
|
+
404 Not Found — Resource doesn't exist
|
|
60
|
+
409 Conflict — Resource conflict (duplicate, version)
|
|
61
|
+
422 Unprocessable — Semantic validation failure
|
|
62
|
+
429 Too Many — Rate limit exceeded
|
|
63
|
+
|
|
64
|
+
Server Errors:
|
|
65
|
+
500 Internal — Unexpected server failure
|
|
66
|
+
502 Bad Gateway — Upstream service failure
|
|
67
|
+
503 Unavailable — Temporarily overloaded or in maintenance
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### HATEOAS (Hypermedia)
|
|
71
|
+
- Include links to related resources and actions
|
|
72
|
+
- Use `_links` or `links` object in responses
|
|
73
|
+
- Enables discoverability — clients follow links, not hardcode URLs
|
|
74
|
+
- Optional but valuable for public APIs
|
|
75
|
+
|
|
76
|
+
## GraphQL
|
|
77
|
+
|
|
78
|
+
### Schema Design
|
|
79
|
+
```graphql
|
|
80
|
+
type Query {
|
|
81
|
+
user(id: ID!): User
|
|
82
|
+
users(filter: UserFilter, first: Int, after: String): UserConnection!
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
type User {
|
|
86
|
+
id: ID!
|
|
87
|
+
name: String!
|
|
88
|
+
email: String!
|
|
89
|
+
orders(first: Int): OrderConnection!
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
# Relay-style pagination
|
|
93
|
+
type UserConnection {
|
|
94
|
+
edges: [UserEdge!]!
|
|
95
|
+
pageInfo: PageInfo!
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Best Practices
|
|
100
|
+
- Design schema from client needs, not database structure
|
|
101
|
+
- Use input types for mutations — clear separation of concerns
|
|
102
|
+
- Implement Relay-style cursor pagination for lists
|
|
103
|
+
- Use DataLoader to batch and deduplicate database queries (N+1 prevention)
|
|
104
|
+
- Set query complexity limits to prevent abuse
|
|
105
|
+
- Use persisted queries for production — security and performance
|
|
106
|
+
|
|
107
|
+
### When to Use Mutations vs Queries
|
|
108
|
+
- Queries for all read operations — cacheable, safe
|
|
109
|
+
- Mutations for writes — clearly express intent with verb naming
|
|
110
|
+
- Subscriptions for real-time — push updates to connected clients
|
|
111
|
+
|
|
112
|
+
## gRPC
|
|
113
|
+
|
|
114
|
+
### Service Definition
|
|
115
|
+
```protobuf
|
|
116
|
+
syntax = "proto3";
|
|
117
|
+
|
|
118
|
+
service UserService {
|
|
119
|
+
rpc GetUser(GetUserRequest) returns (User);
|
|
120
|
+
rpc ListUsers(ListUsersRequest) returns (ListUsersResponse);
|
|
121
|
+
rpc CreateUser(CreateUserRequest) returns (User);
|
|
122
|
+
rpc StreamUpdates(StreamRequest) returns (stream UserEvent);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
message User {
|
|
126
|
+
string id = 1;
|
|
127
|
+
string name = 2;
|
|
128
|
+
string email = 3;
|
|
129
|
+
google.protobuf.Timestamp created_at = 4;
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### Streaming Patterns
|
|
134
|
+
| Pattern | Description | Use Case |
|
|
135
|
+
|---------|-------------|----------|
|
|
136
|
+
| Unary | Single request, single response | Standard RPC calls |
|
|
137
|
+
| Server streaming | Single request, stream of responses | Real-time feeds, logs |
|
|
138
|
+
| Client streaming | Stream of requests, single response | File uploads, bulk data |
|
|
139
|
+
| Bidirectional | Stream both directions | Chat, collaborative editing |
|
|
140
|
+
|
|
141
|
+
### Best Practices
|
|
142
|
+
- Use Protocol Buffers for schema evolution (field numbers are stable)
|
|
143
|
+
- Implement deadlines/timeouts on every call
|
|
144
|
+
- Use interceptors for logging, auth, and metrics (like middleware)
|
|
145
|
+
- Prefer gRPC for internal service-to-service communication
|
|
146
|
+
- Use gRPC-Web or Connect for browser clients
|
|
147
|
+
|
|
148
|
+
## WebSocket
|
|
149
|
+
|
|
150
|
+
### Connection Lifecycle
|
|
151
|
+
```
|
|
152
|
+
Client Server
|
|
153
|
+
|--- HTTP Upgrade ------->|
|
|
154
|
+
|<-- 101 Switching -------|
|
|
155
|
+
| |
|
|
156
|
+
|<== Bidirectional ==> |
|
|
157
|
+
| messages |
|
|
158
|
+
| |
|
|
159
|
+
|--- Close frame -------->|
|
|
160
|
+
|<-- Close frame ---------|
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### Message Patterns
|
|
164
|
+
- **Pub/Sub** — Clients subscribe to topics, server broadcasts
|
|
165
|
+
- **Request/Response** — Client sends request with ID, server responds with matching ID
|
|
166
|
+
- **Event streaming** — Server pushes events as they occur
|
|
167
|
+
|
|
168
|
+
### Best Practices
|
|
169
|
+
- Implement heartbeat/ping-pong for connection health
|
|
170
|
+
- Handle reconnection with exponential backoff
|
|
171
|
+
- Use message framing — JSON or binary with type field
|
|
172
|
+
- Authenticate on connection (token in query or first message)
|
|
173
|
+
- Consider Server-Sent Events (SSE) for server-to-client only
|
|
174
|
+
|
|
175
|
+
## API Versioning
|
|
176
|
+
|
|
177
|
+
| Strategy | Example | Pros | Cons |
|
|
178
|
+
|----------|---------|------|------|
|
|
179
|
+
| URL path | `/v1/users` | Simple, explicit | URL proliferation |
|
|
180
|
+
| Header | `Accept: application/vnd.api+json;v=2` | Clean URLs | Less discoverable |
|
|
181
|
+
| Query param | `/users?version=2` | Easy to test | Clutters params |
|
|
182
|
+
|
|
183
|
+
### Versioning Best Practices
|
|
184
|
+
- Version from day one — even if only `v1`
|
|
185
|
+
- Use URL path versioning for public APIs (most common)
|
|
186
|
+
- Use header versioning for internal APIs
|
|
187
|
+
- Support at most 2 major versions simultaneously
|
|
188
|
+
- Deprecation timeline — announce 6+ months before sunset
|
|
189
|
+
- Non-breaking changes don't require new version (additive fields)
|
|
190
|
+
|
|
191
|
+
## Pagination
|
|
192
|
+
|
|
193
|
+
### Cursor-Based (Recommended)
|
|
194
|
+
```json
|
|
195
|
+
{
|
|
196
|
+
"data": [...],
|
|
197
|
+
"pagination": {
|
|
198
|
+
"next_cursor": "eyJpZCI6MTAwfQ==",
|
|
199
|
+
"has_more": true
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
```
|
|
203
|
+
- Stable under inserts/deletes
|
|
204
|
+
- Efficient with indexed columns
|
|
205
|
+
- Best for infinite scroll, real-time data
|
|
206
|
+
|
|
207
|
+
### Offset-Based
|
|
208
|
+
```json
|
|
209
|
+
{
|
|
210
|
+
"data": [...],
|
|
211
|
+
"pagination": {
|
|
212
|
+
"total": 250,
|
|
213
|
+
"page": 2,
|
|
214
|
+
"per_page": 25
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
```
|
|
218
|
+
- Simpler to implement
|
|
219
|
+
- Supports "jump to page"
|
|
220
|
+
- Inconsistent under concurrent writes
|
|
221
|
+
|
|
222
|
+
## Filtering & Sorting
|
|
223
|
+
|
|
224
|
+
### Filtering Patterns
|
|
225
|
+
```
|
|
226
|
+
GET /users?status=active&role=admin # Simple equality
|
|
227
|
+
GET /users?created_after=2024-01-01 # Range filters
|
|
228
|
+
GET /users?search=john # Full-text search
|
|
229
|
+
GET /users?filter[status]=active # JSON:API style
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
### Sorting
|
|
233
|
+
```
|
|
234
|
+
GET /users?sort=name # Ascending (default)
|
|
235
|
+
GET /users?sort=-created_at # Descending (prefix -)
|
|
236
|
+
GET /users?sort=status,-created_at # Multi-field sort
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
## Error Responses
|
|
240
|
+
|
|
241
|
+
### RFC 7807 Problem Details
|
|
242
|
+
```json
|
|
243
|
+
{
|
|
244
|
+
"type": "https://api.example.com/errors/validation",
|
|
245
|
+
"title": "Validation Error",
|
|
246
|
+
"status": 422,
|
|
247
|
+
"detail": "The email field is not a valid email address.",
|
|
248
|
+
"instance": "/users",
|
|
249
|
+
"errors": [
|
|
250
|
+
{
|
|
251
|
+
"field": "email",
|
|
252
|
+
"message": "Must be a valid email address",
|
|
253
|
+
"code": "INVALID_FORMAT"
|
|
254
|
+
}
|
|
255
|
+
]
|
|
256
|
+
}
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
### Error Design Principles
|
|
260
|
+
- Use consistent error format across all endpoints
|
|
261
|
+
- Include machine-readable error codes
|
|
262
|
+
- Provide human-readable messages
|
|
263
|
+
- Include field-level details for validation errors
|
|
264
|
+
- Never expose stack traces or internal details in production
|
|
265
|
+
|
|
266
|
+
## Rate Limiting
|
|
267
|
+
|
|
268
|
+
### Algorithms
|
|
269
|
+
| Algorithm | How It Works | Best For |
|
|
270
|
+
|-----------|-------------|----------|
|
|
271
|
+
| Token bucket | Tokens added at fixed rate, consumed per request | Burst-tolerant APIs |
|
|
272
|
+
| Sliding window | Count requests in rolling time window | Strict rate enforcement |
|
|
273
|
+
| Fixed window | Count resets at interval boundaries | Simple implementation |
|
|
274
|
+
|
|
275
|
+
### Response Headers
|
|
276
|
+
```
|
|
277
|
+
X-RateLimit-Limit: 100 # Max requests per window
|
|
278
|
+
X-RateLimit-Remaining: 45 # Requests left
|
|
279
|
+
X-RateLimit-Reset: 1640000000 # Window reset time (Unix)
|
|
280
|
+
Retry-After: 30 # Seconds to wait (on 429)
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
## Laravel API Patterns
|
|
284
|
+
|
|
285
|
+
### API Resources (Response Transformation)
|
|
286
|
+
```php
|
|
287
|
+
// Transform Eloquent models into consistent JSON responses
|
|
288
|
+
class UserResource extends JsonResource {
|
|
289
|
+
public function toArray(Request $request): array {
|
|
290
|
+
return [
|
|
291
|
+
'id' => $this->id,
|
|
292
|
+
'name' => $this->name,
|
|
293
|
+
'email' => $this->email,
|
|
294
|
+
'orders_count' => $this->when($this->orders_count !== null, $this->orders_count),
|
|
295
|
+
'created_at' => $this->created_at->toISOString(),
|
|
296
|
+
];
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// Collection with pagination
|
|
301
|
+
return UserResource::collection(User::paginate(25));
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
### Laravel API Best Practices
|
|
305
|
+
- **API Resources** — Never return Eloquent models directly; use Resources for consistent shape
|
|
306
|
+
- **Form Requests** — Validate and authorize in dedicated request classes
|
|
307
|
+
- **Sanctum** — Token-based auth for SPAs and mobile (lightweight alternative to Passport)
|
|
308
|
+
- **Passport** — Full OAuth2 server for third-party API access
|
|
309
|
+
- **API versioning** — Use route prefixes (`/api/v1/`) or header-based
|
|
310
|
+
- **Rate limiting** — Built-in via `RateLimiter` facade and `throttle` middleware
|
|
311
|
+
- **Scribe / Scramble** — Auto-generate API docs from code annotations
|
|
312
|
+
|
|
313
|
+
## Documentation
|
|
314
|
+
|
|
315
|
+
### OpenAPI / Swagger
|
|
316
|
+
- Design API-first — write spec before code
|
|
317
|
+
- Include request/response examples for every endpoint
|
|
318
|
+
- Document all error responses, not just success
|
|
319
|
+
- Use `$ref` for reusable schemas
|
|
320
|
+
- Generate client SDKs from spec (openapi-generator)
|
|
321
|
+
|
|
322
|
+
### Documentation Best Practices
|
|
323
|
+
- Interactive docs (Swagger UI, Stoplight, Redoc)
|
|
324
|
+
- Include authentication setup and getting started guide
|
|
325
|
+
- Provide runnable examples (cURL, SDK snippets)
|
|
326
|
+
- Changelog for API changes
|
|
327
|
+
- Status page for API health
|
|
328
|
+
|
|
329
|
+
## Industry Guidelines Summary
|
|
330
|
+
|
|
331
|
+
### Microsoft REST API Guidelines
|
|
332
|
+
- Use JSON as default format
|
|
333
|
+
- Collections return `{ value: [...] }` wrapper
|
|
334
|
+
- Support `$filter`, `$orderby`, `$top`, `$skip` OData conventions
|
|
335
|
+
- Long-running operations return `202 Accepted` with status URL
|
|
336
|
+
|
|
337
|
+
### Google API Design Guide
|
|
338
|
+
- Resource-oriented design
|
|
339
|
+
- Standard methods: List, Get, Create, Update, Delete
|
|
340
|
+
- Custom methods via `:verb` suffix — `POST /users/{id}:activate`
|
|
341
|
+
- Use field masks for partial updates
|
|
342
|
+
- Consistent naming: camelCase for fields, kebab-case for URLs
|
|
343
|
+
|
|
344
|
+
### Zalando RESTful Guidelines
|
|
345
|
+
- Must use lowercase with hyphens for path segments
|
|
346
|
+
- Must use snake_case for query parameters and JSON fields
|
|
347
|
+
- Must support pagination for collection resources
|
|
348
|
+
- Must use problem JSON (RFC 7807) for errors
|