npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

sammy-api

v1.0.4

Published

Essa lib é usada para consumir arquitetura modular no Next.js, tanto em client ou server side.

Readme

Sammy API

Sammy API is an easy and powerful way to make calls on the client or server in Next.js projects using the new App Directory structure (version 13+). It was designed to work with a well-defined modular architecture, providing a scalable and organized approach for fullstack development.

🚀 Benefits

  • 🌟 Compatible with Next.js App Directory.
  • 🔒 Native support for authentication and authorization with guards.
  • ⚙️ Modular architecture to facilitate maintenance and scalability.
  • 💡 Simplifies communication between client and server.

📦 Installation

You can install the Sammy API library in your project via npm or yarn:

npm install sammy-api

Or:

yarn add sammy-api


📂 Suggested Folder Structure

Below is the suggested folder structure to organize the server modules and frontend:

my-next-app/
├── server/
│   ├── modules/
│   │   ├── user/
│   │   │   ├── controllers/
│   │   │   │   └── user.controller.ts
│   │   │   ├── services/
│   │   │   │   └── user.service.ts
│   │   │   └── user.module.ts
│   │   ├── product/
│   │   │   ├── controllers/
│   │   │   │   └── product.controller.ts
│   │   │   ├── services/
│   │   │   │   └── product.service.ts
│   │   │   └── product.module.ts
│   │   └── app.module.ts
│   ├── api.ts
│   └── main.ts
├── src/
│   └── app/
│      ├── layout.tsx
│      └── page.tsx
└── package.json

💻 Usage Example

1️0️⃣ Guard Layer

import { ISammyGuard, SammyResponse } from 'sammy-api';

export class ApiGuard implements ISammyGuard<IUserLogged> {
  private _allowed?: ERoles[];

  constructor(...roles: ERoles[]) {
    this._allowed = roles;
  }

  async execute() {
    const user = {} as IUserLogged; // implement your logic to fetch the logged-in user
    if (!user) {
      throw SammyResponse.error('User not found');
    }

    const isAuthorized = !!user.roles?.find(role => this._allowed?.includes(role));
    if (!isAuthorized) {
      throw SammyResponse.error('User not authorized');
    }

    return user;
  }
}

1️⃣ Service Layer

In the product.service.ts file:

export class ProductService implements IProductService {
  constructor(private readonly repository: IProductRepository) {}

  async findById(id: string) {
    const product = await this.repository.read(id);
    if (!product) {
      throw SammyResponse.warning('Product not found'');
    }
    return product;
  }
}

2️⃣ Controller Layer

In the user.controller.ts file:

export class ProductController {
  constructor(private readonly service: IProductService) {}

  findById = new SammyProcedure()
    .protect(new ApiGuard(ERoles.USER))
    .execute(async ({ id }, currentUser): SammyResponseDto<ProductDto> => {
      try {
        const product = await this.service.findById(id);
        return SammyResponse.success(product);
      } catch (e) {
        return SammyResponse.error('Error fetching product', e);
      }
    });
}

3️⃣ Module Layer

In the product.module.ts file:

const repo = new ProductRepository();
const service = new ProductService(repo);

const productModule = {
  ...new ProductController(service),
  // other controllers
};

export default productModule;

In the app.module.ts file:

export const serverModules = {
  productModule: import('@module/product'),
  // other modules
} as const;

4️⃣ Server Initialization

In the main.ts file:

'use server'; // IMPORTANT!!!

import { serverModules } from '@module/app';
import { createServer } from 'sammy-api'

export type TApiModules = typeof serverModules;

export async function appServer(...args: any) {
  return createServer(serverModules, ...args);
}

In the api.ts file:

import { createSammy, TModules } from "sammy-api";
import { appServer, TApiModules } from "./main";

export const appApi = createSammy<TModules<TApiModules>>(appServer);

5️⃣ Usage in Frontend

Example on Server Side (page.tsx):

export default async function Page() {
  const { content: product } = await appApi.productModule.findById({ id: "xpto" });
  return (
    <div>
      <h1>{product.name}</h1>
    </div>
  );
}

Example on Client Side (page.tsx):

'use client';
export default async function Page() {
  const [product, setProduct] = useState();

  useEffect(() => {
    appApi.productModule.findById({ id: "xpto" });
      .then(({ content: product }) => setProduct(product))
      .catch(e => console.log(e.message));
  }, []);

  return (
    <div>
      <h1>{product.name}</h1>
    </div>
  );
}

🛠️ Technologies Used

  • Next.js (App Directory - version 13+)
  • Typescript for type safety.

📝 License

This project is under the MIT license. See the LICENSE file for more details.