# sls-relative-paths

> Allow defining Serverless Framework functions handlers paths relatively

Latest version **1.1.1** (published 2022-11-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install sls-relative-paths
pnpm add sls-relative-paths
yarn add sls-relative-paths
bun add sls-relative-paths
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2022-11-02 |
| First published | 2022-08-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 40.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Thomas Aribart |
| Maintainers | thomasaribart |
| Keywords | serverless, framework, function, functions, handler, handlers, path, paths, relative |

## Links

- npm: https://www.npmjs.com/package/sls-relative-paths
- Repository: https://github.com/ThomasAribart/sls-relative-paths
- Homepage: https://github.com/ThomasAribart/sls-relative-paths#readme
- Issues: https://github.com/ThomasAribart/sls-relative-paths/issues
- npm.io page: https://npm.io/package/sls-relative-paths

## Dependencies (2)

- [@babel/runtime](https://npm.io/package/@babel/runtime.md) ^7.18.3
- [@types/serverless](https://npm.io/package/@types/serverless.md) ^3.12.8

## Alternatives

- [base64url](https://npm.io/package/base64url.md) — 6.1M weekly downloads
- [get-installed-path](https://npm.io/package/get-installed-path.md) — 502.9K weekly downloads
- [@uppy/url](https://npm.io/package/@uppy/url.md) — 185.8K weekly downloads
- [@d3fc/d3fc-shape](https://npm.io/package/@d3fc/d3fc-shape.md) — 16.2K weekly downloads
- [localizer](https://npm.io/package/localizer.md) — 226 weekly downloads

## Recent versions

- 1.1.1 (latest) — 2022-11-02
- 1.1.0 — 2022-08-24
- 1.1.0-alpha — 2022-08-24
- 1.0.0 — 2022-08-24
- 0.2.0 — 2022-08-22
- 0.1.4 — 2022-08-22
- 0.0.1 — 2022-08-21

## README

# ✨ DRYer Serverless configuration

When defining a Serverless Framework service file in TS or JS, it's common to import functions configurations from the same folders as their handlers.

It helps keeping the service file light, even when the number of functions grows:

```typescript
// serverless.ts(|js)
import { myFunction } from 'functions/myFunction/config.ts(|js)';
import { anotherFunction } from 'functions/anotherFunction/config.ts(|js)';

export const myService = {
  service: 'my-service',
  functions: {
    myFunction,
    anotherFunction,
    ...
  },
  ...
};
```

However, handlers paths **still need to be provided** in the functions files:

```typescript
// functions/myFunction/handler.ts(|js)
export const main = ... // function code
```

```typescript
// functions/myFunction/config.ts(|js)
export const myFunction = {
  // 👇 Still needed
  handler: 'functions/myFunction/handler.main', // Wait... but that's where I am 😭
  ...
};
```

This is a code duplication that can annoy developers, and frequently cause bugs (typically when moving code around or copy/pasting functions).

That's when `sls-relative-paths` comes to the rescue 💪

## Sls-relative-paths

The `sls-relative-paths` plugin allows you to define your handlers paths **relatively** to their functions files:

```typescript
// serverless.ts(|js)
import { myFunction } from 'functions/myFunction/config.ts';

export const myService = {
  service: 'my-service',
  plugins: ['sls-relative-paths'], // 👈 add plugin
  functions: {
    myFunction,
    ...
  },
  ...
};
```

```typescript
// functions/myFunction/config.ts(|js)
export const myFunction = {
  dirName: __dirname, // 👈 dirname is required to re-construct the complete path
  handler: './handler.main', // 🎉 relative path will work!
  ...
};
```

You can also set a default relative path in your service file:

```typescript
// serverless.ts(|js)
import { myFunction } from 'functions/myFunction/config.ts';

export const myService = {
  service: 'my-service',
  plugins: ['sls-relative-paths'],
  functions: {
    myFunction,
    ...
  },
  relativePaths: {
    default: 'handler.main'
  },
  ...
};
```

```typescript
// functions/myFunction/config.ts(|js)
export const myFunction = {
  dirName: __dirname,
  // 🙌 'handler' prop not needed and defaulted to 'handler.main'
  ...
};
```

## Installation

```bash
# npm
npm install --save-dev sls-relative-paths

# yarn
yarn add --dev sls-relative-paths
```

## Other exports

### `ServiceProperties` type

In TS, you can assign this type to your service file:

```typescript
// serverless.ts(|js)
import type { ServiceProperties } from 'sls-relative-path';
import type { AWS } from '@serverless/typescript';

export const myService: AWS & ServiceProperties = {
  service: 'my-service',
  plugins: ['sls-relative-paths'],
  functions: {
    myFunction,
    ...
  },
  relativePaths: {
    default: ['handler', 'main'] // ❌ default should be a string
  },
  ...
};
```

### `FnProperties` type

In TS, you can assign this type to your functions files:

```typescript
  // functions/myFunction/config.ts(|js)
import type { FnProperties } from 'sls-relative-path';
import type { AWS } from '@serverless/typescript';

type FnConfig = NonNullable<AWS['functions']>[string] & FnProperties;

export const myFunction: FnConfig = {
  dirName: undefined, // ❌ dirName is required
  handler: './handler.main',
  ...
};
```

<p align="right">
  <i>If you use this repo, star it ✨</i>
</p>

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