Skip to content

Conversation

@KhraksMamtsov
Copy link
Contributor

Type

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update

Description

I think in the next iteration it will be better to set the path and schema for it together in order to avoid storing useless PathInput in type parameters of HttpApiEndpoint

Related

  • Related Issue #
  • Closes #

@changeset-bot
Copy link

changeset-bot bot commented Nov 30, 2024

🦋 Changeset detected

Latest commit: 7cb7c14

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 28 packages
Name Type
@effect/platform Patch
@effect/cli Patch
@effect/cluster-node Patch
@effect/cluster-workflow Patch
@effect/cluster Patch
@effect/experimental Patch
@effect/platform-browser Patch
@effect/platform-bun Patch
@effect/platform-node-shared Patch
@effect/platform-node Patch
@effect/rpc-http Patch
@effect/rpc Patch
@effect/sql-clickhouse Patch
@effect/sql-d1 Patch
@effect/sql-libsql Patch
@effect/sql-mssql Patch
@effect/sql-mysql2 Patch
@effect/sql-pg Patch
@effect/sql-sqlite-bun Patch
@effect/sql-sqlite-node Patch
@effect/sql Patch
@effect/ai Patch
@effect/ai-openai Patch
@effect/cluster-browser Patch
@effect/sql-drizzle Patch
@effect/sql-kysely Patch
@effect/sql-sqlite-react-native Patch
@effect/sql-sqlite-wasm Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@tim-smart
Copy link
Contributor

Eventually I want to add something like this:

import { Schema } from "effect"

interface Endpoint<Name extends string, Path extends ReadonlyArray<Schema.Schema.Any>> {
  readonly name: Name
  readonly path: Path
}

type AnySchemaString = Schema.Schema<any, string, never> | Schema.Schema<any, string, any>

class HttpApiEndpoint {
  static get<Name extends string>(_name: Name): <const Path extends ReadonlyArray<AnySchemaString>>(
    strings: TemplateStringsArray,
    ...args: Path
  ) => Endpoint<Name, Path> {
    return (_strings, ..._args) => undefined as any
  }
}

export const endpoint: Endpoint<
  "findById",
  readonly [typeof Schema.NumberFromString]
> = HttpApiEndpoint.get("findById")`/api/${Schema.NumberFromString}`

Just needs to be explored further to see where the limitations are.

@KhraksMamtsov
Copy link
Contributor Author

Yes - it's very elegant
the only point is that I would use aliases rather than positioning for components.
aliases seem to be more intuitive and readable.

class HttpApiEndpoint {
  static get<Name extends string>(
    _name: Name,
  ): <
    const Path extends ReadonlyArray<
      Record<string /* represents alias */, AnySchemaString>
    >,
  >(
    strings: TemplateStringsArray,
    ...args: Path
  ) => Endpoint<Name, Path> {
    return (_strings, ..._args) => undefined as any;
  }
}

export const endpoint: Endpoint<
  "findById",
  readonly [typeof Schema.NumberFromString]
> = HttpApiEndpoint.get("findById")`/api/directory/${{
  directoryId: Schema.NumberFromString,
}}/file/${{ fileId: Schema.NumberFromString }}`;

// client
findById({
  path: { directoryId: 1, fileId: 2 },
  // instead of
  // path: [1, 2]
});

// server
handler: ({ path }) => {
  path.directoryId;
  path.fileId;
};
// instead of
// handler: ({ path }) => {
//   path[0]
//   path[1]
// };

I am sure that at the type level we can guarantee disjoint alias names and that there is only one schema field in the record

@tim-smart
Copy link
Contributor

Hmm using objects feels a bit clunky. Will need to explore it some more.

@tim-smart
Copy link
Contributor

#4035

@tim-smart tim-smart closed this Nov 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants