Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ghc 928 fixes #36

Open
wants to merge 20 commits into
base: ghc-9.2.8
Choose a base branch
from
1 change: 1 addition & 0 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ packages:
./paymentFlow
./api-contract
./dc
./endpoints
2 changes: 1 addition & 1 deletion dc/dc.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,5 @@ test-suite dc-test

-- Test dependencies.
build-depends:
base ^>=4.16.4.0,
base ,
dc
5 changes: 5 additions & 0 deletions endpoints/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Revision history for endpoints

## 0.1.0.0 -- YYYY-mm-dd

* First version. Released on an unsuspecting world.
20 changes: 20 additions & 0 deletions endpoints/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2024 eswar2001

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
127 changes: 127 additions & 0 deletions endpoints/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# Servant API Parser Plugin

A GHC plugin that automatically parses Servant API types and generates API specifications. This plugin analyzes both client and server-side Servant type declarations and converts them into a structured API specification format.

## Features

- Automatically extracts API endpoints from Servant type declarations
- Supports full REST API operations (GET, POST, PUT, DELETE)
- Parses path parameters, query parameters, and headers
- Handles request and response bodies with content type information
- Generates JSON output compatible with API documentation tools

## Output Format

The plugin generates a JSON structure that describes each endpoint with the following information:

```typescript
interface Endpoint {
method: string; // HTTP method
path: string[]; // URL path segments
queryParams: string[]; // Query parameters
headers: string[]; // Required headers
responseStatus: string; // HTTP response status
responseContentType: string | null; // Response content type
responseBody: string | null; // Response body type
requestBody: string | null; // Request body type
requestContentType: string | null; // Request content type
}
```

### Example Output

```json
{
"API": [
{
"headers": [],
"method": "'GET",
"path": ["v1", "api", "users"],
"queryParams": [],
"requestBody": null,
"requestContentType": null,
"responseBody": null,
"responseContentType": "'[JSON]",
"responseStatus": "200"
},
// ... more endpoints
]
}
```

## Implementation Details

The plugin parses Servant types using GHC's Type system and supports the following Servant combinators:

- `:>` - Path concatenation
- `:<|>` - API alternatives
- `Capture'` - URL path parameters
- `QueryParam'` - URL query parameters
- `Header'` - HTTP headers
- `ReqBody'` - Request body
- `Verb` - HTTP methods
- `AddArgs` - Additional arguments
- `Tag` - API grouping tags

### Core Data Types

```haskell
data Endpoint = Endpoint
{ method :: String
, path :: [String]
, queryParams :: [String]
, headers :: [String]
, responseStatus :: String
, responseContentType :: Maybe String
, responseBody :: Maybe String
, requestBody :: Maybe String
, requestContentType :: Maybe String
} deriving (Generic, Show)

data ApiComponent
= Path String
| Capture String
| QueryParam String
| Header String
| AddArgs String
| ReqBody String String
| Verb String String (Maybe String) (Maybe String)
| Group String [ApiComponent]
| Alternative [ApiComponent]
| Tag String
deriving (Generic, Show, Data, ToJSON)
```

## Usage

1. Add the plugin to your project dependencies
2. Enable the plugin in your Cabal file:
```yaml
ghc-options: -fplugin=ServantApiParser
```
3. The plugin will automatically process your Servant API types during compilation
4. Access the generated API specification through the plugin's output

## Example API Definition

```haskell
type UserAPI =
"v1" :> "api" :> "users" :> Get '[JSON] [User]
:<|> "v1" :> "api" :> "users" :> Capture "userId" UserId :> Get '[JSON] User
:<|> "v1" :> "api" :> "users" :> ReqBody '[JSON] User :> Post '[JSON] User
```

## Supported Servant Components

| Component | Description | Example |
|-----------|-------------|---------|
| Path | Static path segments | `"v1" :> "api"` |
| Capture | URL parameters | `Capture "userId" UserId` |
| QueryParam | URL query parameters | `QueryParam "sort" SortOrder` |
| Header | HTTP headers | `Header "Authorization" Token` |
| ReqBody | Request body | `ReqBody '[JSON] User` |
| Verb | HTTP methods | `Get '[JSON] User` |

## Contributing

Feel free to open issues or submit pull requests if you find bugs or have suggestions for improvements. The plugin is designed to be extensible for supporting additional Servant combinators and output formats.
109 changes: 109 additions & 0 deletions endpoints/endpoints.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
cabal-version: 3.0
-- The cabal-version field refers to the version of the .cabal specification,
-- and can be different from the cabal-install (the tool) version and the
-- Cabal (the library) version you are using. As such, the Cabal (the library)
-- version used must be equal or greater than the version stated in this field.
-- Starting from the specification version 2.2, the cabal-version field must be
-- the first thing in the cabal file.

-- Initial package description 'endpoints' generated by
-- 'cabal init'. For further documentation, see:
-- http://haskell.org/cabal/users-guide/
--
-- The name of the package.
name: endpoints

-- The package version.
-- See the Haskell package versioning policy (PVP) for standards
-- guiding when and how versions should be incremented.
-- https://pvp.haskell.org
-- PVP summary: +-+------- breaking API changes
-- | | +----- non-breaking API additions
-- | | | +--- code changes with no API change
version: 0.1.0.0

-- A short (one-line) description of the package.
-- synopsis:

-- A longer description of the package.
-- description:

-- The license under which the package is released.
license: MIT

-- The file containing the license text.
license-file: LICENSE

-- The package author(s).
author: eswar2001

-- An email address to which users can send suggestions, bug reports, and patches.
maintainer: [email protected]

-- A copyright notice.
-- copyright:
category: Development
build-type: Simple

-- Extra doc files to be distributed with the package, such as a CHANGELOG or a README.
extra-doc-files: CHANGELOG.md

-- Extra source files to be distributed with the package, such as examples, or a tutorial module.
-- extra-source-files:

common warnings
ghc-options: -Wall

library
-- Import common warning flags.
import: warnings

-- Modules exported by the library.
exposed-modules: Endpoints.Plugin
default-extensions: OverloadedStrings

-- Modules included in this library but not exported.
-- other-modules:

-- LANGUAGE extensions used by modules in this package.
-- other-extensions:

-- Other library packages from which modules are imported.
build-depends: servant,containers,base,text,directory, bytestring, aeson, extra, aeson-pretty, unordered-containers, uniplate, ghc, references, yaml


-- Directories containing source files.
hs-source-dirs: src

-- Base language which the package is written in.
default-language: Haskell2010

test-suite endpoints-test
-- Import common warning flags.
import: warnings

-- Base language which the package is written in.
default-language: Haskell2010
ghc-options: -fplugin=Endpoints.Plugin
-- Modules included in this executable, other than Main.
-- other-modules:

-- LANGUAGE extensions used by modules in this package.
-- other-extensions:

-- The interface type and version of the test suite.
type: exitcode-stdio-1.0

-- Directories containing source files.
hs-source-dirs: test

-- The entrypoint to the test suite.
main-is: Main.hs

-- Test dependencies.
build-depends:
base ^>=4.16.4.0
, endpoints
, servant
, servant-server
, text
Loading