# @cretadoc/api

> The Cretadoc API.

Latest version **1.0.0** (published 2023-08-28) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @cretadoc/api
pnpm add @cretadoc/api
yarn add @cretadoc/api
bun add @cretadoc/api
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2023-08-28 |
| First published | 2023-08-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=14.6.0 |
| Dependencies | 6 |
| Unpacked size | 644.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Armand Philippot |
| Maintainers | arphi |
| Keywords | cretadoc, graphql, api |

## Links

- npm: https://www.npmjs.com/package/@cretadoc/api
- Repository: https://github.com/ArmandPhilippot/cretadoc
- Homepage: https://github.com/ArmandPhilippot/cretadoc/tree/main/packages/api#readme
- Issues: https://github.com/ArmandPhilippot/cretadoc/issues
- npm.io page: https://npm.io/package/@cretadoc/api

## Dependencies (6)

- [graphql](https://npm.io/package/graphql.md) ^16.8.0
- [validator](https://npm.io/package/validator.md) ^13.11.0
- [dataloader](https://npm.io/package/dataloader.md) ^2.2.2
- [graphql-yoga](https://npm.io/package/graphql-yoga.md) ^4.0.4
- [@cretadoc/utils](https://npm.io/package/@cretadoc/utils.md) ^1.0.1
- [@cretadoc/read-dir](https://npm.io/package/@cretadoc/read-dir.md) ^1.0.1

## Alternatives

- [apollo-link-http-common](https://npm.io/package/apollo-link-http-common.md) — 879.0K weekly downloads
- [react-relay](https://npm.io/package/react-relay.md) — 336.8K weekly downloads
- [relay-test-utils](https://npm.io/package/relay-test-utils.md) — 181.6K weekly downloads
- [@vendure/core](https://npm.io/package/@vendure/core.md) — 14.8K weekly downloads
- [@pnpm/deps.graph-sequencer](https://npm.io/package/@pnpm/deps.graph-sequencer.md) — 13.4K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2023-08-28

## README

# @cretadoc/api

This package provides a GraphQL API for Cretadoc.

## Install

```
npm install @cretadoc/api
```

```
yarn add @cretadoc/api
```

```
pnpm add @cretadoc/api
```

## Usage

We use an [instance of HTTP server](https://nodejs.org/api/http.html#httpcreateserveroptions-requestlistener) as example but you can integrate the API in another way (like an Express middleware) if you want.

### With CommonJS

```cjs
const createAPI = require('@cretadoc/api').createAPI;
const http = require('http');

createAPI()
  .then((api) => {
    const server = http.createServer(api);
    server.listen(4000, () => {
      console.log(`API is available at http://localhost:4000/graphql.`);
    });
  })
  .catch((err) => {
    console.error(err);
  });
```

### With ESM

```mjs
import { createAPI } from '@cretadoc/api';
import { createServer } from 'http';

const api = await createAPI();
const server = createServer(api);
server.listen(4000, () => {
  console.log(`API is available at http://localhost:4000/graphql.`);
});
```

## Configuration

Without a configuration object, the API is not very useful. You should provide the data source(s).

The configuration object looks like:

```ts
export type APIConfig = {
  /**
   * The data configuration.
   * @default undefined
   */
  data?: {
    /**
     * The documentation directory configuration.
     * @default undefined
     */
    doc?: {
      /**
       * The base url to use in file contents for assets and links.
       */
      baseUrl: string;
      /**
       * The path of the documentation directory.
       */
      path: string;
    };
    /**
     * The pages directory configuration.
     * @default undefined
     */
    pages?: {
      /**
       * The base url to use in file contents for assets and links.
       */
      baseUrl: string;
      /**
       * The path of the pages directory.
       */
      path: string;
    };
  };
  /**
   * The API endpoint.
   * @default "/graphql"
   */
  endpoint: string;
  /**
   * Enable GraphiQL.
   * @default true
   */
  graphiql: boolean;
};
```

### Data

The data object allows you to define where the API should look either for pages and/or documentation.

#### Pages

It allows you to retrieve **markdown** files without hierarchy. Typically, you can use the directory to provide contents for your homepage or a legal notice page. To use it, you need to pass **an absolute path to a directory** and a base url. If your router is configured to serve the pages at `/` then you should use this value as baseUrl.

#### Doc

Unlike pages, you can organize your documentation hierarchically. You can provide an **absolute path to a directory** containing many subdirectories which themselves contain many subdirectories and **markdown** files. You also need to define the base url: if your router is configured to serve the documentation at `/doc` then you should use this value as baseUrl.

### Endpoint

This key allows you to define the endpoint to access API.

### GraphiQL

This setting allows you to define if the web interface (GraphiQL) should be accessible.

## Example

```javascript
import { createAPI } from '@cretadoc/api';
import { createServer } from 'http';

const api = await createAPI({
  data: {
    doc: {
      baseUrl: '/doc',
      path: '/absolute/path/to/a/directory',
    },
    pages: {
      baseUrl: '/',
      path: '/absolute/path/to/another/directory',
    },
  },
  endpoint: '/api',
  graphiql: false,
});
const server = createServer(api);
server.listen(6000, () => {
  console.log(`API is available at http://localhost:6000/api.`);
});
```

## Usage

### Metadata

Each pages and documentation entries can contain metadata through front matter.

However, you can't use any key. Only the following ones are accepted:

- createdAt (if not provided, it will be set automatically)
- seoDescription
- seoTitle
- status (`draft` or `published` expected)
- title
- updatedAt (if not provided, it will be set automatically)

**Example:**

```md
---
title: My awesome page
status: published
---

This is the main content of the page.
```

**Note:** If you use an empty string (`''`) when using a mutation to update a file/directory, if the key is already set, it will be removed.

### Excerpt

You can also define an excerpt by using a separator: `<!-- excerpt -->`.

**Example:**

```md
This is an excerpt.<!-- excerpt -->This is the main content of the page.
```

### Documentation directories

The directories can also handle metadata, excerpt and text contents. All you need to do is to create a special file named `index.md`.

Note:

- if you use the API to create the directory, the file will be automatically created,
- this file won't be listed in the directory's files.

## License

This package is released under the [MIT license](./LICENSE).

The documentation (Markdown files) is released under a [Creative Commons license](./LICENSE-docs).

---
_Source: https://npm.io/package/@cretadoc/api · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
