Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sdk): Add AssemblyScript SDK #10

Merged
merged 2 commits into from
Aug 23, 2023
Merged

Conversation

FranklinWaller
Copy link
Member

Motivation

Adding this allows developers to develop data requests using AssemblyScript

Explanation of Changes

  • Add AssemblyScript functions (http & process)
  • Add integration tests

Testing

npm test

Related PRs and Issues

closes #2

.devcontainer/Dockerfile Outdated Show resolved Hide resolved
libs/as-sdk-integration-tests/project.json Outdated Show resolved Hide resolved
libs/as-sdk-integration-tests/package.json Outdated Show resolved Hide resolved
libs/as-sdk-integration-tests/src/http.test.ts Outdated Show resolved Hide resolved
libs/as-sdk/assembly/__test__/http.spec.ts Outdated Show resolved Hide resolved
libs/as-sdk/assembly/__test__/http.spec.ts Outdated Show resolved Hide resolved
libs/as-sdk/assembly/index.ts Outdated Show resolved Hide resolved
@@ -0,0 +1,16 @@
import { JSON } from '../../../node_modules/assemblyscript-json/assembly/index';

export function jsonArrToUint8Array(array: JSON.Arr): Uint8Array {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like a good candidate for some unit tests?


const response = String.UTF8.decode(responseBuffer);

return PromiseStatus.fromStr(response, new HttpResponse, new HttpResponse);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still bothers me 😛

Quick doodle I came up with:

type FromBuffer<T> = (buffer: Uint8Array) => T;

export class PromiseStatus<F, R> {
  private constructor(
    public fulfilled: F | null,
    public rejected: R | null
  ) {}

  isFulfilled(): this is true {
    return this.fulfilled !== null;
  }

  isRejected() {
    return this.rejected !== null;
  }

  static fromStr<F, R>(promiseStatus: string, fulfilled: FromBuffer<F>, rejected: FromBuffer<R>): PromiseStatus<F, R> {
    const value = JSON.parse(promiseStatus);
    let fulfilledResult: F | null = null;
    let rejectedResult: R | null = null;

    const rawFulfilled = value.getArr('Fulfilled');
    if (rawFulfilled) {
      fulfilledResult = fulfilled(rawFulfilled);
    }

    const rawRejected = value.getArr('Rejected');
    if (rawRejected) {
      rejectedResult = rejected(rawRejected);
    }

    return new PromiseStatus(fulfilledResult, rejectedResult);
  }
}

class HTTPResponseSuccess {
  public readonly success = true;
  constructor(public buffer: Uint8Array) {};
}

class HTTPResponseFailure {
  public readonly failure = true;
  constructor(public buffer: Uint8Array) {};
}

function newSuccess(buffer: Uint8Array) {
  return new HTTPResponseSuccess(buffer);
}

function newFailure(buffer: Uint8Array) {
  return new HTTPResponseFailure(buffer);
}

const promiseStatus = PromiseStatus.fromStr('a', newSuccess, newFailure);

if (promiseStatus.isFulfilled()) {
  promiseStatus.fulfilled?.success;
} else {
  promiseStatus.rejected?.failure;
}

The main thing I still dislike is that the isFulfilled check still doesn't let TS infer that the property is not null.

Not saying this is the way to go but hopefully this triggers some new ideas on how to improve this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also don't mind the implementation in fromStr, I made a few edits so my editor would shut up.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest we can just ditch the isFulfilled and isRejected, i'm also not really using them.

Copy link
Member

@Thomasvdam Thomasvdam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The remaining open comments are more (really) nice to haves.

@FranklinWaller FranklinWaller force-pushed the feat/assembly-script-sdk branch 3 times, most recently from 4c7c887 to 08af076 Compare August 23, 2023 09:31
@FranklinWaller FranklinWaller merged commit 5cb9a01 into main Aug 23, 2023
1 check passed
@FranklinWaller FranklinWaller deleted the feat/assembly-script-sdk branch August 23, 2023 09:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

✨ AssemblyScript SDK
2 participants