An open API service indexing awesome lists of open source software.

https://github.com/istyle-inc/example-hack-strict-api

Example. HHVM/Hack Nazg Framework Response Assert
https://github.com/istyle-inc/example-hack-strict-api

hack hhvm hypermedia-api nazg-framework

Last synced: 5 months ago
JSON representation

Example. HHVM/Hack Nazg Framework Response Assert

Awesome Lists containing this project

README

          

# Nazg Framework - Response Type Assert Sample

Example). **Hal, Type Assert Middleware, Cors**

## Get Strated

```bash
$ git clone https://github.com/istyle-inc/example-hack-strict-api.git
$ hhvm -d xdebug.enable=0 -d hhvm.jit=0 -d hhvm.php7.all=1\
-d hhvm.hack.lang.auto_typecheck=0 $(which composer) install
```

## Example

```bash
$ curl http://example-hack-strict-api.vagrant/
```

### Response

```
Content-Type application/hal+json
```

```json
{
"id": 1234,
"name": "ytake",
"title": "type-assert for api response",
"_links": {
"self": {
"href": "/",
"type": "application/json"
}
},
"_embedded": {
"enviroments": [
{
"name": "HHVM/Hack",
"_links": {
"self": {
"href": "https://docs.hhvm.com/"
}
}
},
{
"name": "zend-diactoros",
"_links": {
"self": {
"href": "https://zendframework.github.io/zend-diactoros/"
}
}
}
]
}
}
```

### Response Type Assert Middleware

```hack
string,
'_links' => shape(
'self' => shape(
'href' => string
)
)
);

const type hateoasStructure = shape(
'id' => int,
'name' => string,
'title' => string,
'_links' => shape(
'self' => shape(
'href' => string,
'type' => string
)
),
'_embedded' => shape(
'enviroments' => array
)
);

public function process(
ServerRequestInterface $request,
RequestHandlerInterface $handler,
): ResponseInterface {
$response = $handler->handle($request);
$decode = json_decode($response->getBody()->getContents(), true);
TypeAssert\matches_type_structure(
type_structure(self::class, 'hateoasStructure'),
$decode,
);
return $response;
}
}

```